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 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 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
divide(dinero, value) View Source
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}
divide(dinero, value, round_up) View Source
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}
multiply(dinero, value) View Source
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}
multiply(dinero, value, round_up) View Source
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}
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(1.0e4, :USD)
%Dinero{amount: 1000000, currency: :USD}
iex> Dinero.new(100, :RUR)
** (ArgumentError) currency RUR not found
parse(amount, currency \\ :USD) View Source
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
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}