PTAX.Money (ptax v0.4.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

@type currency() :: atom()
@type t() :: %PTAX.Money{amount: Decimal.t(), currency: currency()}

Link to this section Functions

Link to this function

exchange!(money, list)

@spec exchange!(money, to: currency(), rate: money) :: money when money: 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), 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}
Link to this function

new(amount, currency \\ :BRL)

@spec new(amount :: any(), currency()) :: t()

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}