Monetized v0.1.0 Monetized.Money
Defines the money struct and functions to handle it.
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.
Examples
config :monetized, config: [
delimiter: ",",
separator: ".",
currency: "USD",
format: "%c %n%s%d"
]
Summary
Functions
Creates a money struct from a float value
Creates a money struct from a integer value
Creates a money struct from a string value
Creates a money struct from any of the supported types for amount
Returns a string representation of the given money
Types
money :: %Monetized.Money{currency: term, units: term}
A money struct containing the basic unit amount and the currency key.
Functions
Specs
from_float(float, list) :: money
Creates a money struct from a float value.
It uses the default currency (“USD”) if one isn’t configured.
Passing currency in the options will make it use that despite of configured or default.
This function exists for convenience and despite it taking a float value, the internal calculations are done on integers (basic units)
Examples
iex> Monetized.Money.from_float(100.00, [currency: "EUR"])
%Monetized.Money{currency: "EUR", units: 10000}
iex> Monetized.Money.from_float(150.52)
%Monetized.Money{currency: "USD", units: 15052}
iex> Monetized.Money.from_float(20.50)
%Monetized.Money{currency: "USD", units: 2050}
Specs
from_integer(integer, list) :: money
Creates a money struct from a integer value.
It uses the default currency (“USD”) if one isn’t configured.
Passing currency in the options will make it use that despite of configured or default.
Examples
iex> Monetized.Money.from_integer(152, [currency: "GBP"])
%Monetized.Money{currency: "GBP", units: 15200}
iex> Monetized.Money.from_integer(152, [currency: "GBP"])
%Monetized.Money{currency: "GBP", units: 15200}
Creates a money struct from a string value.
It uses the default currency (“USD”) if one isn’t configured.
Passing currency in the options will make it use that despite of configured or default.
Examples
iex> Monetized.Money.from_string("10.52", [currency: "GBP"])
%Monetized.Money{currency: "GBP", units: 1052}
iex> Monetized.Money.from_string("100", [currency: "EUR"])
%Monetized.Money{currency: "EUR", units: 10000}
Creates a money struct from any of the supported types for amount.
It uses the default currency (“USD”) if one isn’t configured.
Passing currency
in the options will make it use that
despite of configured or default.
This function exists for convenience and despite it taking a float value, the internal calculations are done on integers (basic units)
Examples
iex> Monetized.Money.make("20150.25", [currency: "GBP"])
%Monetized.Money{currency: "GBP", units: 2015025}
iex> Monetized.Money.make(20150.25, [currency: "EUR"])
%Monetized.Money{currency: "EUR", units: 2015025}
iex> Monetized.Money.make(20150)
%Monetized.Money{currency: "USD", units: 2015000}
iex> Monetized.Money.make(-100.50)
%Monetized.Money{currency: "USD", units: -10050}
Returns a string representation of the given money
Examples
iex> money = Monetized.Money.make("20150.25", [currency: "GBP"])
...> Monetized.Money.to_string(money, [show_currency: true])
"£ 20,150.25"
iex> money = Monetized.Money.make(999999999)
...> Monetized.Money.to_string(money, [delimiter: " ", separator: " "])
"999 999 999 00"
iex> money = Monetized.Money.make(100_000_000, [currency: "USD"])
...> Monetized.Money.to_string(money, [format: "%n%s%d %c", show_currency: true])
"100,000,000.00 $"
iex> money = Monetized.Money.make(-9950, [currency: "USD", units: true])
...> Monetized.Money.to_string(money, [show_currency: true])
"$ -99.50"