josior · 2022-05-21 13:49
I want to sum the values of a table column like this:
|$ 10.123,45|
|$ 5.134,32 |
|$ 123,18 |
|-----------|
| |
It seems that the currency character ("$"), and the thousand separator in the value prevent Org from summing the values, and I haven't found an option to customize that.
The workaround I found was to replace `query-regex-replace` the "$", and dots with nothing to remove them. But I was wondering about possible alternatives. Any ideas?
Edit: noticed that it doesn't calculate correctly with a comma as a decimal separator, I must use a dot.
Thanks!
dm_g · 2022-05-21 19:08
I have been curious on how table calculations work. I was able to put something together...
but, the $ at the front a number gets Org into a loop (I think it tries to find the cell with that number (e.g. a value $20 will try to get the value from column $20). There must be a space between the $ and the number.
Please see below. You will need to change the period to a comma for European numbers. I am sure there must a global variable that keeps that info, though.
#+begin_src elisp
(defun keep-only-numeric (st)
(string-to-number
(replace-regexp-in-string "[^0-9\.]" "" st)))
(defun filter-numbers (lst)
(mapcar 'keep-only-numeric lst))
(defun add-numbers (v &rest lst)
(+ (keep-only-numeric v)
(reduce '+ (filter-numbers lst))))
(add-numbers "3.0" "abc" "$20.0")
#+end_src
| 1 | ¥2,100.4 | 3 | 4 | 2101.4 |
| 2 | $ 3 | 10 | 5.20 | 5 |
#+TBLFM: $5='(add-numbers $1..$2)'
Phil-Hudson · 2022-05-21 19:25
Is it important to you to have the `$` character in the cell? Can you manage without it? Org doesn't recognize such cells as numeric.
josior · 2022-05-21 20:53
It's not important to have it in the cell, I could manage without it.
josior · 2022-05-21 20:57
I tried to run this with `C-c C-c` in the table, inserts an `#ERROR` in the last column.
dm_g · 2022-05-22 22:38
make sure you load the elisp functions before you execute the code. You can also debug the executions of a formula using C-c {