dinero v1.1.1 Dinero View Source

Dinero is a struct that provides methods for working with currencies

Examples

iex> d1 = Dinero.new(100, :USD)
%Dinero{amount: 10000, currency: :USD}
iex> d2 = Dinero.new(200, :USD)
%Dinero{amount: 20000, currency: :USD}
iex> Dinero.add(d1, d2)
%Dinero{amount: 30000, currency: :USD}

Note: Dinero uses coins value for calculations. So when you create a new Dinero struct with 100 USD it automatically transforms this into 10000 cents

Link to this section Summary

Functions

Adds two Dinero structs

Converts value of Dinero to target currency using exchange_rate

Divides Dinero by a value and rounds the result

Multiplies a Dinero by a value. If a multiplier is float you can pass the third boolean param to round up (true) or truncate (false, default) the result

Creates a new Dinero struct with provided currency. If currency is not supported, ArgumentError will be raised

Subtracts one Dinero from another

Link to this section Types

Link to this type

t() View Source
t() :: %Dinero{amount: integer(), currency: atom()}

Link to this section Functions

Link to this function

add(dinero1, dinero2) View Source
add(t(), t()) :: t()

Adds two Dinero structs

Examples

iex> d1 = Dinero.new(100, :USD)
%Dinero{amount: 10000, currency: :USD}
iex> d2 = Dinero.new(20, :USD)
%Dinero{amount: 2000, currency: :USD}
iex> Dinero.add(d1, d2)
%Dinero{amount: 12000, currency: :USD}
Link to this function

convert(d, target, exchange_rate) View Source
convert(t(), atom() | String.t(), float()) :: t()

Converts value of Dinero to target currency using exchange_rate

Examples

iex> d = Dinero.new(100, :USD)
%Dinero{amount: 10000, currency: :USD}
iex> Dinero.convert(d, :UAH, 26.2)
%Dinero{amount: 262000, currency: :UAH}
iex> Dinero.convert(d, :USD, 26.2)
** (ArgumentError) target currency must be different from source currency
Link to this function

divide(dinero, value) View Source
divide(t(), integer() | float()) :: t()

Divides Dinero by a value and rounds the result

Examples

iex> d = Dinero.new(100.24, :USD)
%Dinero{amount: 10024, currency: :USD}
iex> Dinero.divide(d, 3)
%Dinero{amount: 3341, currency: :USD}
iex> Dinero.divide(d, 5)
%Dinero{amount: 2005, currency: :USD}
Link to this function

multiply(dinero, value, round_up \\ false) View Source
multiply(t(), integer() | float(), boolean()) :: t()

Multiplies a Dinero by a value. If a multiplier is float you can pass the third boolean param to round up (true) or truncate (false, default) the result

Examples

iex> d = Dinero.new(120, :USD)
%Dinero{amount: 12000, currency: :USD}
iex> Dinero.multiply(d, 4)
%Dinero{amount: 48000, currency: :USD}
iex> d = Dinero.new(100, :USD)
%Dinero{amount: 10000, currency: :USD}
iex> Dinero.multiply(d, 1.005, true)
%Dinero{amount: 10050, currency: :USD}
iex> Dinero.multiply(d, 1.005)      
%Dinero{amount: 10049, currency: :USD}
Link to this function

new(amount, currency) View Source
new(integer() | float(), atom() | String.t()) :: t()

Creates a new Dinero struct with provided currency. If currency is not supported, ArgumentError will be raised

Examples

iex> Dinero.new(100, :USD)
%Dinero{amount: 10000, currency: :USD}
iex> Dinero.new(100, :RUR)
** (ArgumentError) currency RUR not found
Link to this function

subtract(dinero1, dinero2) View Source
subtract(t(), t()) :: t()

Subtracts one Dinero from another

Examples

iex> d1 = Dinero.new(100, :USD)
%Dinero{amount: 10000, currency: :USD}
iex> d2 = Dinero.new(20, :USD)
%Dinero{amount: 2000, currency: :USD}
iex> Dinero.subtract(d1, d2)
%Dinero{amount: 8000, currency: :USD}