dinero v1.0.0 Dinero View Source
Dinero is a struct that provides methods for working with currencies
Examples
iex> d = Dinero.new(100, :USD)
%Dinero{amount: 10000, currency: :USD}
iex> Dinero.add(d, 200)
%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
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
Link to this section Types
Link to this section Functions
add(arg1, value) View Source
Adds two Dinero structs or Dinero and integer or float value.
Both integer and float values will be converted to cents
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}
iex> Dinero.add(d1, 10)
%Dinero{amount: 11000, currency: :USD}
iex> Dinero.add(d1, 0.05)
%Dinero{amount: 10005, 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(arg1, value) View Source
Subtracts one Dinero from another or an integer/float (converted to cents) from a Dinero
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}
iex> Dinero.subtract(d1, 10)
%Dinero{amount: 9000, currency: :USD}
iex> Dinero.subtract(d1, 10.24)
%Dinero{amount: 8976, currency: :USD}