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 Money.
Documentation can be found at https://hexdocs.pm/money/Money.html on HexDocs
five_eur=Money.new(500,:EUR)# %Money{amount: 500, currency: :EUR}ten_eur=Money.add(five_eur,five_eur)# %Money{amount: 10_00, currency: :EUR}hundred_eur=Money.multiply(ten_eur,10)# %Money{amount: 100_00, currency: :EUR}ninety_nine_eur=Money.subtract(hundred_eur,1)# %Money{amount: 99_00, currency: :EUR}shares=Money.divide(ninety_nine_eur,2)[%Money{amount: 50,currency: :EUR},%Money{amount: 49,currency: :EUR}]Money.equals?(five_eur,Money.new(500,:EUR))# trueMoney.zero?(five_eur);# falseMoney.positive?(five_eur);# trueMoney.Currency.symbol(:USD)# $Money.Currency.symbol(Money.new(500,:AFN))# ؋Money.Currency.name(Money.new(500,:AFN))# AfghaniMoney.to_string(Money.new(500,:CNY))# ¥ 5.00Money.to_string(Money.new(1_234_56,:EUR),separator: ".",delimeter: ",",symbol: false)"1.234,56"Money.to_string(Money.new(1_234_56,:USD),fractional_unit: false)# "$1,234"# Sigils for MoneyimportMoney.Sigilsiex>~M[1000]USD%Money{amount: 1000,currency: :USD}# If you have a default currency configured (e.g. to GBP), you can doiex>~M[1000]%Money{amount: 1000,currency: :GBP}# Currency convenience methodsimportMoney.Currency,only: [usd: 1,eur: 1,afn: 1]iex>usd(100_00)%Money{amount: 10000,currency: :USD}iex>eur(100_00)%Money{amount: 10000,currency: :EUR}iex>afn(100_00)%Money{amount: 10000,currency: :AFN}Money.Currency.symbol(:USD)# $Money.Currency.symbol(afn(500))# ؋Money.Currency.name(afn(500))# AfghaniMoney.Currency.get(:AFN)# %{name: "Afghani", symbol: "؋"}Bring Money to your Ecto project.
The underlying database type is integer
# migrationcreatetable(:my_table)doadd:amount,:integerend# model/schemaschema"my_table"dofield:amount,Money.Ecto.TypeendBring Money to your Phoenix project.
If you are using Phoenix, you can include money objects directly into your output and they will be correctly escaped.
<b><%=Money.new(12345,67,:GBP)%></b>Money comes with no required dependencies.
Add the following to your mix.exs:
defdepsdo[{:money,"~> 1.3.1"}]endthen run mix deps.get.
You can set a default currency and default formatting preferences as follows:
config:money,default_currency: :EUR,separator: ".",delimeter: ",",symbol: false,symbol_on_right: false,symbol_space: falseThen you don’t have to specify the currency.
iex>amount=Money.new(1_234_50)%Money{amount: 123450,currency: :EUR}iex>to_string(amount)"1.234,50"Here is another example of formatting money:
iex>amount=Money.new(1_234_50)%Money{amount: 123450,currency: :EUR}iex>Money.to_string(amount,symbol: true,symbol_on_right: true,symbol_space: true)"1.234,50 €"MIT License please see the LICENSE file.