Last edit
Summary: separator should've been op in order for this to work as claimed
Changed:
< (match-string 1 num) separator
to
> (match-string 1 num) op
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"