all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* elisp to put commas a number?
@ 2008-06-12 14:36 The Quiet Center
  2008-06-12 16:20 ` Rupert Swarbrick
  2008-06-13 12:29 ` Herbert Euler
  0 siblings, 2 replies; 4+ messages in thread
From: The Quiet Center @ 2008-06-12 14:36 UTC (permalink / raw)
  To: help-gnu-emacs

Hello, does anyone have any code to put commas in a number?

The emacswiki code <http://www.emacswiki.org/cgi-bin/wiki/
ElispCookbook#toc10>
did not work.

Here is an example of what I want:

(commify (* 130 70491))
9,163,830



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: elisp to put commas a number?
  2008-06-12 14:36 elisp to put commas a number? The Quiet Center
@ 2008-06-12 16:20 ` Rupert Swarbrick
  2008-06-13 12:29 ` Herbert Euler
  1 sibling, 0 replies; 4+ messages in thread
From: Rupert Swarbrick @ 2008-06-12 16:20 UTC (permalink / raw)
  To: help-gnu-emacs


[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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: elisp to put commas a number?
  2008-06-12 14:36 elisp to put commas a number? The Quiet Center
  2008-06-12 16:20 ` Rupert Swarbrick
@ 2008-06-13 12:29 ` Herbert Euler
  2008-06-13 12:36   ` Herbert Euler
  1 sibling, 1 reply; 4+ messages in thread
From: Herbert Euler @ 2008-06-13 12:29 UTC (permalink / raw)
  To: The Quiet Center, help-gnu-emacs


How about this one?

(defun commify (n)
  (let ((list '()))
    (while (> n 0)
      (setq list (cons (format "%d" (% n 1000)) list)
	    n (/ n 1000)))
    (mapconcat (lambda (s) s) list ",")))

Regards,
Guanpeng Xu
_________________________________________________________________
Connect to the next generation of MSN Messenger 
http://imagine-msn.com/messenger/launch80/default.aspx?locale=en-us&source=wlmailtagline



^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: elisp to put commas a number?
  2008-06-13 12:29 ` Herbert Euler
@ 2008-06-13 12:36   ` Herbert Euler
  0 siblings, 0 replies; 4+ messages in thread
From: Herbert Euler @ 2008-06-13 12:36 UTC (permalink / raw)
  To: The Quiet Center, help-gnu-emacs


Sorry, that doesn't cover the case of n = 0.  This one:

(defun commify (n)
  (if (= n 0)
      "0"
    (let ((list '()))
      (while (> n 0)
	(setq list (cons (format "%d" (% n 1000)) list)
	      n (/ n 1000)))
      (mapconcat (lambda (s) s) list ","))))

Regards,
Guanpeng Xu
_________________________________________________________________
Connect to the next generation of MSN Messenger 
http://imagine-msn.com/messenger/launch80/default.aspx?locale=en-us&source=wlmailtagline



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-06-13 12:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-12 14:36 elisp to put commas a number? The Quiet Center
2008-06-12 16:20 ` Rupert Swarbrick
2008-06-13 12:29 ` Herbert Euler
2008-06-13 12:36   ` Herbert Euler

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.