all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Elisp version of CL: (format t "~:d" NUM)
@ 2021-05-21 19:02 Jean Louis
  2021-05-21 19:44 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 2+ messages in thread
From: Jean Louis @ 2021-05-21 19:02 UTC (permalink / raw)
  To: Help GNU Emacs

In Common Lisp it is very easy to get readable comma separated digits:

(clisp-macro (format t "~:d" 123456890)) ⇒ "123,456,890"

As from:
http://www.lispworks.com/documentation/HyperSpec/Body/22_cbb.htm

In Emacs Lisp I have made this below, did not tested it much yet. And
I think this function is way too big. Maybe there is some simpler way?

(defun number-comma-char (num)
  "For NUM return readable comma separated digits."
  (let* ((num (cond ((numberp num) (number-to-string num))
		    (t num)))
	 (decimal (string-match "\\." num))
	 (rest (if decimal (substring num decimal) ""))
	 (num (if decimal (substring num 0 decimal) num))
	 (count 1)
	 (list)
	 (chain (seq-map 'identity (reverse num))))
    (while chain
      (let ((char (pop chain)))
	(cond ((and (= (truncate (/ count 3.0)) (/ count 3.0))
		    chain)
	       (progn
		 (push char list)
		 (push ?, list)))
	      (t (push char list)))
	(setq count (1+ count))))
    (concat (mapconcat 'char-to-string list "") rest)))

(number-comma-char 123456.0) ⇒ "123,456.0"



Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

Sign an open letter in support of Richard M. Stallman
https://stallmansupport.org/



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

* Re: Elisp version of CL: (format t "~:d" NUM)
  2021-05-21 19:02 Elisp version of CL: (format t "~:d" NUM) Jean Louis
@ 2021-05-21 19:44 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 2+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-05-21 19:44 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

> In Common Lisp it is very easy to get readable comma
> separated digits:

https://lists.gnu.org/archive/html/help-gnu-emacs/2008-06/msg00369.html

> (defun number-comma-char (num)
>  "For NUM return readable comma separated digits."
>  (let* ((num (cond ((numberp num) (number-to-string num))
>         (t num)))
>   (decimal (string-match "\\." num))
>   (rest (if decimal (substring num decimal) ""))
>   (num (if decimal (substring num 0 decimal) num))
>   (count 1)
>   (list)
>   (chain (seq-map 'identity (reverse num))))
>    (while chain
>      (let ((char (pop chain)))
>  (cond ((and (= (truncate (/ count 3.0)) (/ count 3.0))
>         chain)
>         (progn
>      (push char list)
>      (push ?, list)))
>        (t (push char list)))
>  (setq count (1+ count))))
>    (concat (mapconcat 'char-to-string list "") rest)))

Haha you are always so much fun with this semi-manual style of
yours :)

But good work...

--
underground experts united
https://dataswamp.org/~incal




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

end of thread, other threads:[~2021-05-21 19:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-05-21 19:02 Elisp version of CL: (format t "~:d" NUM) Jean Louis
2021-05-21 19:44 ` Emanuel Berg via Users list for the GNU Emacs text editor

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.