Emacs, scripting and anything text oriented.

Field Formatters in Org table

Kaushal Modi

Using field-formatters to format results of Org table formulae.

If you have a simple Org table like:

| 0.5 |
| 1.5 |
|-----|
|     |

And you want the A3 cell to contain the sum of A1 and A2 cells, you would add this at the bottom on that table and evaluate it (C-c C-c).

#+tblfm: @>$1=vsum(@1..@>>)

That formula reads as: “Set the value of the cell is last row (@>)1, first column ($1) be equal to the sum of all cells in the same column from row 1 (@1) to second-to-the-last row (@>>).”

But then you end up with an odd-looking 2. instead of 2.0 in the result cell:

| 0.5 |
| 1.5 |
|-----|
|  2. |
#+tblfm: @>$1=vsum(@1..@>>)

So I had posted a question on the Org mailing list to understand if this was a bug — it was not.

Using printf style %0.1f formatter #

Thanks to the reply from Thierry Banel to that question, one of the solutions is to use field formatters, like in printf in C (and many other languages):

| 0.5 |
| 1.5 |
|-----|
| 2.0 |
#+tblfm: @>$1=vsum(@1..@>>);%0.1f

Using Calc f1 formatter #

Another solution, also from Thierry, was to use the Calc f1 formatter instead of %0.1f (and similarly f5 instead of %0.5f).

| 0.5 |
| 1.5 |
|-----|
| 2.0 |
#+tblfm: @>$1=vsum(@1..@>>);f1
Note
While the %.1f format is handy for those who are used to printf() syntax, note that Calc unlimited precision numbers are converted to double floats before applying %.1f. Whereas f1 operates on Calc numbers without conversion.

  1. See (org) References for more information on those field references. ↩︎

Webmentions
Mentioned by Kaushal Modi on Tue Apr 24, 2018 00:11 EDT
Org Table Spreadsheet
—Published on Mon Apr 2, 2018

Normalizing #

Table 1: Org table Normalizing Normalized col $1 Normalized col $3 Ratio of col $2 / $4 Col $1 Col $2 Col $3 Col $4 Col $5 79922.1 6.85 0.146993824 4.27 1.60 79185.9 6.78 0.146976008 4.27 1.59 64041.0 5.48 0.147067031 4.27 …