In EmacsWiki, sub page "Add Commas To Numbers", it says:
Here is how to transform a number such as 12345 into the string “12,345”.
(defun add-number-grouping (number &optional separator) "Add commas to NUMBER and return it as a string. Optional SEPARATOR is the string to use to separate groups. It defaults to a comma." (let ((num (number-to-string number)) (op (or separator ","))) (while (string-match "\\(.*[0-9]\\)\\([0-9][0-9][0-9].*\\)" num) (setq num (concat (match-string 1 num) op (match-string 2 num)))) num))
Note that you can also specify a different separator:
(add-number-grouping 12345 "'") => "12'345"
That's nice, and it works!
We've done some minor code adjustments, simplifying it,
so that it only takes a dot .
as a separator,
not a comma or possibly anything else.
|----------+------------+---|
| a | b | c |
| 4 | 12 | |
|----------+------------+---|
| 2 | 4 | |
| 79 | 1 | |
| 33 | 99 | |
| 2 | 55 | |
| c | n | |
| r | 9 | |
| 9 | 8 | |
|----------+------------+---|
| 84729823 | 84.729.823 | |
| 3248 | 3.248 | |
| 22 | 22 | |
| 34458 | 34.458 | |
| 1298869 | 1.298.869 | |
|----------+------------+---|
| 7 | 8 | |
| 4 | 9 | |
| 31 | 779 | |
|----------+------------+---|
#+TBLFM: @III$2..@IIII$2='(add-number-grouping $1);N
Even in Orgmode spreadsheets with wanting it to operate just on certain column ranges.
But the thing is this:
Where turning 12.521.876
into 12521876
is needed,
need for the reverse, turning 12521876
back into 12.521.876
again,
is not far away.
With my bloody-elisp-beginner skills, it didn't take me long to figure out the reverse the manual way.
|----+------------+----------|
| a | b | c |
| 4 | 12 | |
|----+------------+----------|
| 2 | 4 | |
| 79 | 1 | |
| 33 | 99 | |
| 2 | 55 | |
| c | n | |
| r | 9 | |
| 9 | 8 | |
|----+------------+----------|
| | 84.729.823 | 84729823 |
| | 3.248 | 3248 |
| | 22 | 22 |
| | 34.458 | 34458 |
| | 1.298.869 | 1298869 |
|----+------------+----------|
| 7 | 8 | |
| 4 | 9 | |
| 31 | 779 | |
|----+------------+----------|
#+TBLFM: @III$3..@IIII$3='(replace-regexp-in-string "\\.\\([0-9][0-9][0-9]\\)" "\\1" $2)
And now the problem:
I want to place add-number-grouping
into my Emacs initial config file,
so that this function is always at hand
and doesn't need to be coded again manually
before invocation; and just like that I want if for the reverse, remove-number-grouping
.
But I just fail miserably in doing the deceptively simple step to take what I already have – that last Elisp formula – and turning it into a proper function! And it's probably really, because I'm totally new to Elisp and don't actually know what I'm doing there. I'm coming from bash, feel comfy with it, and accompanied by tiny peek at Python, looking at Elisp code I'm seeing traces of familiar basic concepts like arguments, variables, loops, but the Elisp syntax just breaks me – it's so cryptic and the error messages don't really help.
1st attempt – minor changes on add-number-grouping code as thought fit:
(defun remove-number-grouping (string)
(let ((str "string"))
(replace-regexp-in-string "\\.\\([0-9][0-9][0-9]\\)" "\\1" str)
str))
Hitting RET
echo area in Emacs is saying remove-number-grouping
.
Great!
Now invoking it,…
M-;
in Emacs spawns eval function in the bottom line for
evaluating, using functions
prompted for input, I type in (remove-n TAB
, and it auto-completes! That's a good sign, it knows what we're up to!
(remove-number-grouping
and then continuing to
(remove-number-grouping 12.553)
hit RET
what we wish we got
"12553"
what we actually got
remove-number-grouping: Wrong type argument: sequencep, 12.553
Attempt #2: input:
Eval: (defun remove-number-grouping (string)
(replace-regexp-in-string "\\.\\([0-9][0-9][0-9]\\)" "\\1" string))
M-;
(remove-number-grouping 33.552)
RET
output:
remove-number-grouping: Wrong type argument: sequencep, 33.552
another error message often appearing in my attempts to make it work:
Symbol's value as variable is void: remove-number-grouping
That popped up often, as well:
eval: Symbol's value as variable is void: 12\.424\.989
Done so for hours. What's the solution?
What's the problem, that Elisp is not happy when I'm giving it the
function remove-number-grouping
and a value to work with, like 56.6454.32.34242
?