PTAX.Money (ptax v1.0.0)
Defines a Money
structure for working with currencies.
Link to this section Summary
Functions
Exchange from a currency to another given the rate.
Create a new Money
given the amount and currency.
Link to this section Types
Link to this type
currency()
@type currency() :: atom()
Link to this section Functions
Link to this function
exchange!(money, pair)
@spec exchange!(money, [to: currency(), rate: money] | pair) :: money when money: t(), pair: PTAX.Money.Pair.t()
Exchange from a currency to another given the rate.
Examples:
iex> PTAX.Money.exchange!(PTAX.Money.new("12.75", :USD), to: :BRL, rate: PTAX.Money.new(5, :BRL))
%PTAX.Money{amount: Decimal.new("63.75"), currency: :BRL}
iex> PTAX.Money.exchange!(PTAX.Money.new(10), to: :USD, rate: PTAX.Money.new(5, :BRL))
%PTAX.Money{amount: Decimal.new(2), currency: :USD}
iex> PTAX.Money.exchange!(PTAX.Money.new(10), to: :USD, rate: PTAX.Money.new("0.2", :USD))
%PTAX.Money{amount: Decimal.new("2.0"), currency: :USD}
iex> PTAX.Money.exchange!(PTAX.Money.new(123, :GBP), to: :GBP, rate: PTAX.Money.new("1", :GBP))
** (PTAX.Error) Cannot exchange to the same currency!
iex> PTAX.Money.exchange!(PTAX.Money.new(1, :USD), to: :GBP, rate: PTAX.Money.new(2, :USD))
%PTAX.Money{amount: Decimal.new("0.5"), currency: :GBP}
iex> PTAX.Money.exchange!(PTAX.Money.new("119.50", :JPY), PTAX.Money.Pair.new(Decimal.new("119.50"), :USD, :JPY))
%PTAX.Money{amount: Decimal.new(1), currency: :USD}
iex> PTAX.Money.exchange!(PTAX.Money.new(1, :USD), PTAX.Money.Pair.new(Decimal.new("119.50"), :USD, :JPY))
%PTAX.Money{amount: Decimal.new("119.50"), currency: :JPY}
Link to this function
new(amount, currency \\ :BRL)
Create a new Money
given the amount and currency.
Examples:
iex> PTAX.Money.new(10)
%PTAX.Money{amount: Decimal.new(10), currency: :BRL}
iex> PTAX.Money.new("12.75", :USD)
%PTAX.Money{amount: Decimal.new("12.75"), currency: :USD}
iex> PTAX.Money.new(123, :GBP)
%PTAX.Money{amount: Decimal.new(123), currency: :GBP}
Link to this function