Elisp Pretty Numbers

30 Nov 2011

Here are some elisp functions I cooked up today to make looking at numbers from within emacs a little more convenient.

First, a function to add a thousands separator to a (possibly floating point) number:

(defun my-thousands-separate (num)
  "Formats the (possibly floating point) number with a thousands
  (let* ((nstr (number-to-string num))
         (dot-ind (string-match "\\." nstr))
         (nstr-no-decimal (if dot-ind
                               (substring nstr 0 dot-ind)
         (nrest (if dot-ind
                    (substring nstr dot-ind)
         (pretty nil)
         (cnt 0))
    (dolist (c (reverse (append nstr-no-decimal nil)))
      (if (and (zerop (% cnt 3)) (> cnt 0))
          (setq pretty (cons ?, pretty)))
      (setq pretty (cons c pretty))
      (setq cnt (1+ cnt)))
    (concat pretty nrest)))


(my-thousands-separate 4324.32)
  ==> "4,324.32"
(my-thousands-separate 42)
  ==> "42"
(my-thousands-separate 929344324432444.0)
  ==> "929,344,324,432,444.0"

With that function in hand, I wrote a little convenience function to help maintain sanity while looking at ginourmous numbers:

(defun my-prettify-number (n)
  "Prints a number to the minibuffer in a few delicious
formats. If `current-word' is a number, that's what is used,
otherwise we prompt the user."
   (let ((default
             (skip-chars-backward "0-9")
             (if (looking-at "[-+]?\\([0-9]*\.\\)?[0-9]+")
                 (string-to-number (current-word))
               (read-number "Number: ")))))
     (list default)))
  (let ((nstr (number-to-string n))
        (npretty (my-thousands-separate n)))
    (message "%s | %g | %d | %s" npretty n n nstr)))

Usage: put your point on a number and do M-x my-prettify-number.

blog comments powered by Disqus