Monetized v0.5.0 Monetized.Money
Defines the money struct and functions to handle it.
Also defines Money
Ecto.Type.
Although we’re able to override any configuration when calling functions that create/handle money, it is possible to change any of the default values seen below, through config.
Below are the configuration options.
Examples
config :monetized, config: [
delimiter: ",",
separator: ".",
currency: "USD",
format: "%c %n%s%d"
]
Summary
Functions
Casts the given value to money
Converts an Monetized.Money
into a string for
saving to the db. ie: “100.50 EUR”
Creates a money struct from a Decimal
Creates a money struct from a float value
Creates a money struct from a integer value
Creates a money struct from a string value
Converts a string as saved to the db into a
Monetized.Money
struct
Creates a money struct from any of the supported types for amount
Returns a string representation of the given money
The Ecto primitive type
Creates a money struct with 0 value
Types
Functions
Casts the given value to money.
It supports:
- A string (if currency not relevant).
- A float (if currency not relevant).
- An
Decimal
struct (if currency not relevant). - An integer (if currency not relevant).
- A map with
:value
and:currency
keys. - A map with “value” and “currency” keys.
- An
Monetized.Money
struct.
Converts an Monetized.Money
into a string for
saving to the db. ie: “100.50 EUR”
Specs
from_decimal(Decimal, list) :: t
Creates a money struct from a Decimal.
It uses the default currency (“USD”) if one isn’t configured.
Passing currency in the options will make it use that despite of configured default.
Examples
iex> Decimal.new(100.00) |> Monetized.Money.from_decimal([currency: "EUR"])
#Money<100.00EUR>
iex> Decimal.new(150.52) |> Monetized.Money.from_decimal
#Money<150.52>
iex> Decimal.new("300.25") |> Monetized.Money.from_decimal
#Money<300.25>
Specs
from_float(float, list) :: t
Creates a money struct from a float value.
Passing currency in the options will make it use that despite of configured default.
Examples
iex> Monetized.Money.from_float(100.00, [currency: "EUR"])
#Money<100.00EUR>
iex> Monetized.Money.from_float(150.52)
#Money<150.52>
# iex> Monetized.Money.from_float(20.50)
#Money<20.50>
Specs
from_integer(integer, list) :: t
Creates a money struct from a integer value.
Passing currency in the options will make it use that despite of configured default.
Examples
iex> Monetized.Money.from_integer(152, [currency: "GBP"])
#Money<152.00GBP>
iex> Monetized.Money.from_integer(100_000, [currency: "GBP"])
#Money<100000.00GBP>
iex> Monetized.Money.from_integer(-100, [currency: "GBP"])
#Money<-100.00GBP>
Creates a money struct from a string value.
Passing currency in the options will make it use that despite of configured default.
Examples
iex> Monetized.Money.from_string("GBP 10.52")
#Money<10.52GBP>
iex> Monetized.Money.from_string("€ 100")
#Money<100.00EUR>
iex> Monetized.Money.from_string("100.00", [currency: "EUR"])
#Money<100.00EUR>
iex> Monetized.Money.from_string("$50")
#Money<50.00USD>
iex> Monetized.Money.from_string("1,000,000 EUR")
#Money<1000000.00EUR>
iex> Monetized.Money.from_string("200", currency: "THB")
#Money<200.00THB>
Converts a string as saved to the db into a
Monetized.Money
struct.
Creates a money struct from any of the supported types for amount.
If a string is given with either the currency key/code (ie “USD”) or the symbol present, that currency will be assumed.
Passing currency
in the options will make it use that
despite of configured, or assumed from string.
Examples
iex> Monetized.Money.make("20150.25 EUR")
#Money<20150.25EUR>
iex> Monetized.Money.make(20150.25, [currency: "EUR"])
#Money<20150.25EUR>
iex> Decimal.new("100.50") |> Monetized.Money.make
#Money<100.50>
iex> Monetized.Money.make("£ 100")
#Money<100.00GBP>
# currency in options takes precedence
iex> Monetized.Money.make("€ 50", [currency: "USD"])
#Money<50.00USD>
Returns a string representation of the given money.
Examples
iex> money = Monetized.Money.make("£ 20150.25")
...> Monetized.Money.to_string(money, [currency_symbol: true])
"£ 20,150.25"
# Ignores currency as there isn't one
iex> money = Monetized.Money.make(999999999)
...> Monetized.Money.to_string(money, [delimiter: " ", separator: " ", currency_symbol: true])
"999 999 999 00"
iex> money = Monetized.Money.make(100_000_000, [currency: "USD"])
...> Monetized.Money.to_string(money, [format: "%n%s%d %cs", currency_symbol: true])
"100,000,000.00 $"
iex> money = Monetized.Money.make(-99.50, [currency: "USD"])
...> Monetized.Money.to_string(money, [currency_symbol: true])
"$ -99.50"
iex> money = Monetized.Money.make("100.50 EUR")
...> Monetized.Money.to_string(money, [currency_code: true])
"100.50 EUR"
iex> money = Monetized.Money.make(Decimal.new("10"))
...> Monetized.Money.to_string(money)
"10.00"
iex> money = Monetized.Money.make(Decimal.new("10"), currency: "USD")
...> Monetized.Money.to_string(money, currency_symbol: true)
"$ 10.00"
iex> money = Monetized.Money.make(Decimal.new(".005"), currency: "USD")
...> Monetized.Money.to_string(money, currency_symbol: true)
"$ 0.01"