1

According to the org mode documentation for calc syntax you can append printf formatters to specify the number of decimal places to display ‘$1+$2;%.2f’ Same, format result to two decimals. Is there a way to set this as the default for all floats? The org-calc-default-modes variables for precision do not correspond to printf precision (digits after the decimal point), but instead to significant digits.

2

"precision" refers to the number of floating point digits Calc does its internal computations with. The floating point format specifies how many digits to print after the decimal point. The default value is

(defcustom org-calc-default-modes
  '(calc-internal-prec 12
               calc-float-format  (float 8)
               calc-angle-mode    deg
               calc-prefer-frac   nil
               calc-symbolic-mode nil
               calc-date-format (YYYY "-" MM "-" DD " " Www (" " hh ":" mm))
               calc-display-working-message t)
  "List with Calc mode settings for use in `calc-eval' for table formulas.
The list must contain alternating symbols (Calc modes variables and values).
Don't remove any of the default settings, just change the values.  Org mode
relies on the variables to be present in the list."
  :group 'org-table-calculation
  :type 'plist)

If you want two digits after the decimal point, you have to change calc-float-format to (fix 2):

(plist-put org-calc-default-modes 'calc-float-format '(fix 2))

For example, here's a table with a formula evaluated using the default org-calc-default-modes (followed by some code blocks: the first code block shows the current value of org-calc-default-modes; the second resets the calc-float -format-entry to (fix 2)):


|  x |    exp(x) |
|----+-----------|
|  1 | 2.7182818 |
|  2 | 7.3890561 |
|  3 | 20.085537 |
|  4 | 54.598150 |
|  5 | 148.41316 |
|  6 | 403.42879 |
|  7 | 1096.6332 |
|  8 | 2980.9580 |
|  9 | 8103.0839 |
| 10 | 22026.466 |
#+TBLFM: $2=exp($1)

* code
#+begin_src emacs-lisp :results drawer
org-calc-default-modes
#+end_src

#+RESULTS:
:results:
(calc-internal-prec 12 calc-float-format (float 8) calc-angle-mode deg calc-prefer-frac nil calc-symbolic-mode nil calc-date-format (YYYY - MM - DD   Www (  hh : mm)) calc-display-working-message t calc-hms-format %s:%s:%s)
:end:

#+begin_src emacs-lisp
(plist-put org-calc-default-modes 'calc-float-format '(fix 2)
#+end_src

Now hit C-c C-c on the second code block and then C-c C-c in the formula and the first code block - here's what I get:

* table

|  x |   exp(x) |
|----+----------|
|  1 |     2.72 |
|  2 |     7.39 |
|  3 |    20.09 |
|  4 |    54.60 |
|  5 |   148.41 |
|  6 |   403.43 |
|  7 |  1096.63 |
|  8 |  2980.96 |
|  9 |  8103.08 |
| 10 | 22026.47 |
#+TBLFM: $2=exp($1)

* code
#+begin_src emacs-lisp :results drawer
org-calc-default-modes
#+end_src

#+RESULTS:
:results:
(calc-internal-prec 12 calc-float-format (fix 2) calc-angle-mode deg calc-prefer-frac nil calc-symbolic-mode nil calc-date-format (YYYY - MM - DD   Www (  hh : mm)) calc-display-working-message t calc-hms-format %s:%s:%s)
:end:

#+begin_src emacs-lisp :results drawer
(plist-put org-calc-default-modes 'calc-float-format '(fix 2))
#+end_src

#+RESULTS:
:results:
(calc-internal-prec 12 calc-float-format (fix 2) calc-angle-mode deg calc-prefer-frac nil calc-symbolic-mode nil calc-date-format (YYYY - MM - DD   Www (  hh : mm)) calc-display-working-message t calc-hms-format %s:%s:%s)
:end:

See Float formats in the Calc manual.

EDIT: You are right that the above link is not enough to come up with the answer. calc-float-format is an Org mode construct: it's what Org mode uses to communicate its wishes to Emacs Calc, so it's Org mode that's responsible for setting it correctly. So I peeked: the code for the formula modifiers (like ;%0.2f) looks like this:

        (setq org-tbl-calc-modes
              (org-table--set-calc-mode
               'calc-float-format
               (list (cdr (assoc c '((?n . float) (?f . fix)
                         (?s . sci) (?e . eng))))
                 n))))

That was enough for me, although YMMV: it will depend on how well you can read lisp.

2
  • Excellent answer, thank you! In the link you posted, I still wouldn't have been able to come to this solution. Is there additional info needed to deduce your solution? – young_souvlaki Jul 3 '20 at 13:40
  • Edited the answer with a bit of additional info. – NickD Jul 3 '20 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.