==========
== 0x2f ==
==========

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:

  1. start calc by typing C-x * c
  2. type in any integer without a unit, for example: 100 RET. We have just 100 on the calc stack now.
  3. press u 1 to apply the unit EUR to the `100` we wrote. We have 100 EUR on the calc stack now.
  4. press u 3 to convert to the USD unit. We now have 117 USD on 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] RET then s 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.