dinero v1.1.0 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 section Functions
add(dinero1, dinero2) View Source
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}
convert(d, target, exchange_rate) View Source
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
divide(dinero, value) View Source
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}
multiply(dinero, value, round_up \\ false) View Source
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}
new(amount, currency) View Source
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
subtract(dinero1, dinero2) View Source
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}