PTAX.Money (ptax v1.1.1)

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 amount() :: Decimal.t()
@type currency() :: atom()
@type pair() :: PTAX.Money.Pair.t()
@type t() :: %PTAX.Money{amount: amount(), currency: currency()}

Link to this section Functions

Link to this function

exchange(money, pair)

@spec exchange(money, pair()) :: money when money: t()

Exchange from a currency to another given the rate.

examples

Examples:

iex> PTAX.Money.exchange(PTAX.Money.new(10, :USD), PTAX.Money.Pair.new(5, "5.5", :USD, :BRL))
PTAX.Money.new(50, :BRL)

iex> PTAX.Money.exchange(PTAX.Money.new(1, :USD), PTAX.Money.Pair.new("1.5", 2, :GBP, :USD))
PTAX.Money.new("0.5", :GBP)

iex> PTAX.Money.exchange(PTAX.Money.new(115, :JPY), PTAX.Money.Pair.new("114.5", 115, :USD, :JPY))
PTAX.Money.new(1, :USD)

iex> PTAX.Money.exchange(PTAX.Money.new(1, :USD), PTAX.Money.Pair.new("114.5", 115, :USD, :JPY))
PTAX.Money.new("114.5", :JPY)
Link to this function

new(amount, currency \\ :BRL)

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

Create a new Money given the amount and currency.

examples

Examples:

iex> PTAX.Money.new(10)
%PTAX.Money{amount: PTAX.Money.to_amount(10), currency: :BRL}

iex> PTAX.Money.new("12.75", :USD)
%PTAX.Money{amount: PTAX.Money.to_amount("12.75"), currency: :USD}

iex> PTAX.Money.new(123, :GBP)
%PTAX.Money{amount: PTAX.Money.to_amount(123), currency: :GBP}
Link to this function

to_amount(value, places \\ 4)

@spec to_amount(value :: value(), places :: integer() | nil) :: amount()