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))))

Note that you can also specify a different separator:

    (add-number-grouping 12345 "'")
    => "12'345"