[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: org-tables with monetary amounts
From: |
Derek Feichtinger |
Subject: |
Re: org-tables with monetary amounts |
Date: |
Tue, 13 Oct 2020 09:10:30 +0200 |
User-agent: |
mu4e 1.4.1; emacs 26.2 |
Hi,
On Mon, Oct 12 2020, Eric S Fraga <e.fraga@ucl.ac.uk> wrote:
> On Monday, 12 Oct 2020 at 10:22, Christian Moe wrote:
>> I think I was thinking about adding mode flags for unit computations,
>> like in the imagined example above (`$3=$2+$1;u'), similar to what has
>> been done for duration computations.
>
> This would be very useful, actually. I use embedded calc all the time
> with units; being able to use some of the same expressions in a table
> would be highly welcome.
The functions that one can use in calc formulas can be extended using
the 'defmath' lisp command. I paste here from my notes (not about
monetary units, but easily adapted):
+++++++++++++++++++++++++++++++
Defining a new calc function for unit conversion with defmath
#+BEGIN_SRC emacs-lisp
(defmath uconv (expr target-units &optional pure)
(math-convert-units expr target-units pure))
#+END_SRC
#+RESULTS:
: calcFunc-uconv
| km | ft |
|--------+--------------|
| 2.5 km | 8202.0997 ft |
#+TBLFM: $2=uconv($1, ft)
Using the units from the table header (if the 3rd arg is given to
uconv, the output is stripped of the unit):
| km | ft |
|-----+-----------|
| 2.5 | 8202.0997 |
#+TBLFM: $2 = uconv($1 * @<$1, @<$2, t)
The standard calc function usimplify also works for this use
case:
| km | ft |
|-----+-----------|
| 2.5 | 8202.0997 |
#+TBLFM: $2 = usimplify($1 * @<$1 / @<$2)
A lisp equivalent of the above
#+begin_src elisp
(calc-eval "usimplify(2.5 km / ft)")
#+end_src
#+RESULTS:
: 8202.09973753
Let's define a function that converts to base units
#+begin_src elisp
(defmath ustd (expr) (math-simplify-units (math-to-standard-units expr nil)))
#+end_src
#+RESULTS:
: calcFunc-ustd
| distance | time | speed | std unit speed | speed in ft/s |
|----------+--------+-------------+------------------+------------------|
| 3 km | 2.5 hr | 1.2 km / hr | 0.33333333 m / s | 1.0936133 ft / s |
#+TBLFM: @2$3=$1/$2::@2$4=ustd($3)::@2$5=uconv($-1, ft/s)
++++++++++++++++++++++++++++++++++++
--
Paul Scherrer Institut
Dr. Derek Feichtinger Phone: +41 56 310 47 33
Group Head HPC and Emerging Technologies Email: derek.feichtinger@psi.ch
Building/Room No. OHSA/D17
Forschungsstrasse 111
CH-5232 Villigen PSI