help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: elisp to put commas a number?


From: Rupert Swarbrick
Subject: Re: elisp to put commas a number?
Date: Thu, 12 Jun 2008 17:20:01 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux)

[Dammit, hit 'r' rather than 'f' in Gnus. Sorry "The Quiet Center"
that you'll get this twice]

The Quiet Center <thequietcenter@gmail.com> writes:
> Hello, does anyone have any code to put commas in a number?

> ...

> Here is an example of what I want:
>
> (commify (* 130 70491))
> 9,163,830


'Ow about this?

(defun commify (n &optional comma-char)

 (unless comma-char (setq comma-char ","))

 (with-temp-buffer
   (insert (format "%s" n))
   (while (> (- (point)
                (line-beginning-position))
             (if (>= n 0) 3 4))
     (backward-char 3)
     (insert comma-char)
     (backward-char 1))
   (buffer-string)))


Try:

(commify (* 130 70491) ".")
(commify (* -13 70491))


I just wrote it, so fingers crossed it does the right thing! Not sure
whether there's a way to find the locale's choice for comma-char,
though.


Rupert


reply via email to

[Prev in Thread] Current Thread [Next in Thread]