Elixir library for working with Money safer, easier, and fun, is an interpretation of the Fowler's Money pattern in fun.prog.
"If I had a dime for every time I've seen someone use FLOAT to store currency, I'd have $999.997634" -- Bill Karwin
In short: You shouldn't represent monetary values by a float. Wherever you need to represent money, use this Money.
alias Money, as: M
five_eur = M.eur(500);
ten_eur = M.add(five_eur, five_eur);
ten_eur_div_2 = M.divide(ten_eur, 2);
M.equals?(ten_eur_div_2, five_eur); # true
M.zero?(five_eur); # false
M.currency_symbol(:USD) # $
M.currency_symbol(M.afn(500)) # ؋
M.currency_name(M.afn(500)) # Afghani
M.to_string(M.cny(500)) # ¥ 5.00
Money comes with no dependencies, is still in dev state.
Install the library using mix deps.get. Add the following to your mix.exs
:
def deps do
[ { :money, "~> 0.0.1-dev" } ]
end
Now run the mix deps.get
command.
After you are done, run mix deps.get
in your shell to fetch and compile Decimal. Start an interactive Elixir shell with iex -S mix
.
iex> alias Money, as: M
nil
iex> M.usd(1000)
%Money{amount: 1000, currency: :USD}
iex> M.add(M.eur(500), M.eur(400))
%Money{amount: 900, currency: :EUR}
MIT License please see the LICENSE file.
- doc
- options: round_up or round_down now is the simple round
- create the money type as struct