Is there an Erlang library to deal with Money (avoid floats arithmetics) like this one in Elixir:
It’s not clear to me from the README.md exactly what the Elixir Money
type offers. When I added Money support to my own Smalltalk library,
I found it darned near impossible to get right without three classes:
- Currency
- Money (amount, currency)
- Purse (dictionary[currency->amount])
converting Money or a Purse to a specific Currency requires also a
DateAndTime because exchange rates vary; it’s possible for
m1 comparedWith: m2 at: then
and
m1 comparedWith: m2 at: now
to disagree. It’s at this point that I gave up because getting
historic exchange rates required a paid subscription to an exchange
rate service and also required dealing with the fact that converting
let’s say Ottoman currency to Euros as of 1893 is challenging because
Euros didn’t exist then (although of course the animals called euros
had existed for a long time).
This poses a particular problem for a functional language.
Let M1 be an amount of currency in USD and M2 an amount in EUR.
The value of less_than(M1, M2) can change, even though nothing
in the program caused it to change.
I respectfully suggest baby steps. Start with an encapsulation of
money in a single currency, the currency of the locale (LC_MONETARY).
That avoids all exchange rate issues and all problems with currencies
not existing at the same time.
The library seems to be only about money, that is, amount and currency. The currency is mainly a set of representation and formatting rules. Also, there is some sugar and candy around that. Operations like addition, subtraction, comparison etc works only between same currencies, no exchange rates or anything.
How about eapa
?
It’s a (Rust) NIF but unfortunately it doesn’t compile.
Hm, in your initial post you asked for a Money library. eapa
, working or not, is a general arbitrary-precision library, not specific to money (which typically includes the concept of currencies). So, I wonder, what are you actually trying to accomplish, ie what are the requested features of such a library?
Pure Erlang code for scaled decimal arithmetic is
not particularly difficult. (I have a paper on
specifying fixed-point arithmetic, using Haskell
and QuickCheck to verify the specification. I
could never find a home for it.) The problem is
deciding which operations (other than basic
arithmetic and comparison) to include and what
things like division should mean. And also
the fact that a lot of algebraic laws are not
true: x - y + y is numerically equal to x but
may have different precision from x, which has
additional consequences downstream.
Libraries/language facilities for money can be
misleading. If your currency is dollars and
cents, keeping money as a whole number of cents
is a no-brainer. That is, you do that if you
have no brain. Because what are you going to do
about percentages?
In Smalltalk, it’s easy. Just keep money as a
rational number of cents and percentages as
rational numbers and throw in explicit roundings
when you must. In Erlang, with bignums but not
ratios, it’s not so easy. As a first approximation,
represent money as
{CURrency, Numerator, Denominator}.