More convenient currency conversion with Emacs calc-currency package
It’s possible to make currency conversion faster than described in the readme file of the calc-currency package:
This isn’t the place for a full Calc tutorial, but you can play around as follows: To start Calc up, type C-x * c. ’ 25000 JPY Enter (the apostrophe key, then the text “25000 JPY”, then the Enter key) will put 25000 Japanese yen on the Calc stack. u c USD Enter will convert the value on the stack to US dollars.
Quick units
(setq var-Units '(vec (var EUR var-EUR) (var PLN var-PLN) (var USD var-USD)))
Now the process from the readme file simplifies to:
- start calc by typing
C-x * c - type in any integer without a unit, for example:
100 RET. We have just100on the calc stack now. - press
u 1to apply the unitEURto the `100` we wrote. We have100 EURon the calc stack now. - press
u 3to convert to theUSDunit. We now have117 USDon the calc stack.
This is way faster because you don’t have to type in the currency as an algebraic expression by pressing `’` in calc and manually typing out the full currency code (EUR).
This uses two Calc features:
- calc variables - in calc type in algebraic
' [USD,EUR] RETthens s Units RET - “quick units”
Because we want to persist the calc variable “Units” between sessions we `setq` it rather than having to set them up from scratch every time
Avoiding pressing `’` all the time
You can make whatever you start typing into calc and algebraic expression by default to avoid hitting ' all the time, just run M-x calc and type m a
If you don’t even want to visit calc
If you use calc-currency for simple one off conversions and would prefer not to even visit M-x calc at all, you can use
(defun rtz/quickconvert (from to)
(interactive "sAmount and currency: \nsConvert %s to: ")
(calc-currency-load)
(message (calc-eval (math-convert-units (calc-eval from 'raw) (calc-eval to 'raw)))))
Then M-x rtz/quickconvert, type e.g. 100 USD RET then EUR RET. This is not guaranteed to use the latest currency data because the calc-start hook doesn’t run.
It looks like it’s not possible to do currency conversion in (quick-calc) (C-x * q) because all the unit conversion functions expect there to be something on the stack, so this is why a separate function is necessary.