dinero v1.2.2 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 and truncates the result

Divides Dinero by a value and truncates the result

Divides Dinero by a value and rounds up the result

Multiplies a Dinero by a value and truncates the result

Multiplies a Dinero by a value and rounds up the result

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

Creates Dinero from String that represents integer or float. If a string can't be parsed ArgumentError is raised If the second param is not provided it uses USD as default currency

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 and truncates the result

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 truncates 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: 2004, currency: :USD}
Link to this function

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

Divides Dinero by a value and rounds up the result

Examples

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

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

Multiplies a Dinero by a value and truncates 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)      
%Dinero{amount: 10049, currency: :USD}
Link to this function

multiply(dinero, value, round_up) View Source
multiply(t(), integer() | float(), atom()) :: t()

Multiplies a Dinero by a value and rounds up the result

Examples

iex> d = Dinero.new(100, :USD)
%Dinero{amount: 10000, currency: :USD}
iex> Dinero.multiply(d, 1.005, :round_up)
%Dinero{amount: 10050, 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(1.0e4, :USD) 
%Dinero{amount: 1000000, currency: :USD}
iex> Dinero.new(100, :RUR)
** (ArgumentError) currency RUR not found
Link to this function

parse(amount, currency \\ :USD) View Source
parse(String.t(), atom()) :: t()

Creates Dinero from String that represents integer or float. If a string can't be parsed ArgumentError is raised If the second param is not provided it uses USD as default currency

Examples

iex> Dinero.parse("123.23") %Dinero{amount: 12323, currency: :USD} iex> Dinero.parse("112") %Dinero{amount: 11200, currency: :USD} iex> Dinero.parse("2", :UAH) %Dinero{amount: 200, currency: :UAH} iex> Dinero.parse("100.00")
%Dinero{amount: 10000, currency: :USD} iex> Dinero.parse("invalid string") ** (ArgumentError) invalid string. it must contain string that represents integer or float

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}