Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing thousand separator in org-mode spreadsheets

Tags:

emacs

org-mode

Is there a way to configure org-mode spreadsheets to show thousand separators? I would like 1000 to be shown as 1,000.

like image 645
jacobilsoe Avatar asked May 09 '15 13:05

jacobilsoe


People also ask

What are org mode files?

Org Mode (also: org-mode; /ˈɔːrɡ moʊd/) is a document editing, formatting, and organizing mode, designed for notes, planning, and authoring within the free software text editor Emacs.

How do you save in Org mode?

To save the document, either press the save icon, or press C-x C-s, call it 1.org. Emacs does not actually understand you are editing an Org document, yet.

How do you make a table in Org mode?

The easiest way to create a table is to directly type the "|" character at the beginning of a line, or after any amount of white space. This will put you in the first field of an atomic table. Once you've finished editing this cell, you can jump to the next one by pressing TAB .


1 Answers

Here is a solution. First define the function group-number:

(defun group-number (num &optional size char)
    "Format NUM as string grouped to SIZE with CHAR."
    ;; Based on code for `math-group-float' in calc-ext.el
    (let* ((size (or size 3))
           (char (or char ","))
           (str (if (stringp num)
                    num
                  (number-to-string num)))
            ;; omitting any trailing non-digit chars
            ;; NOTE: Calc supports BASE up to 36 (26 letters and 10 digits ;)
           (pt (or (string-match "[^0-9a-zA-Z]" str) (length str))))
      (while (> pt size)
        (setq str (concat (substring str 0 (- pt size))
                          char
                          (substring str (- pt size)))
              pt (- pt size)))
      str))

from: Emacs Cookbook - put commas in numbers

Next, use it in your formula:
|           |       |             |
|-----------+-------+-------------|
|      2000 |  3100 |       5,100 |
|        11 |    22 |          33 |
| 111221212 | 29996 | 111,251,208 |
|           |       |           0 |
#+TBLFM: $3='(group-number (+ $1 $2));N

Here we are using the group-number function to format the result of an emacs lisp form - rather than using the calc formula syntax.

like image 90
Jeffrey DeLeo Avatar answered Sep 17 '22 08:09

Jeffrey DeLeo