A small money helper around Decimal amounts and a currency string.
Money is represented throughout Logistiki as a %Decimal{} amount paired
with a currency string. This module provides convenience constructors and
guards. Amounts are always positive in postings; the debit/credit direction
encodes sign.
Fields
amount—Decimal.t()— the monetary amountcurrency—String.t()— the currency code (e.g."USD")
Example
iex> m = Logistiki.Accounting.Money.new("1000.00", "USD")
%Logistiki.Accounting.Money{amount: Decimal.new("1000.00"), currency: "USD"}
Summary
Functions
Adds two money values of the same currency.
Negates the amount.
Builds a money value, coercing numeric inputs to Decimal.
True when the amount is greater than zero.
Subtracts b from a (same currency).
Converts the amount to integer cents (rounded down).
Returns the zero amount for currency.
True when the amount equals zero.
Types
Functions
Adds two money values of the same currency.
Arguments
a—%__MODULE__{}.b—%__MODULE__{}— must have the samecurrencyasa.
Returns
%__MODULE__{}—a.amount + b.amountin the shared currency.
Examples
iex> a = Logistiki.Accounting.Money.new("100", "USD")
iex> b = Logistiki.Accounting.Money.new("50", "USD")
iex> Logistiki.Accounting.Money.add(a, b)
%Logistiki.Accounting.Money{amount: Decimal.new("150"), currency: "USD"}
Negates the amount.
Arguments
m—%__MODULE__{}.
Returns
%__MODULE__{}— withamountnegated.
Examples
iex> m = Logistiki.Accounting.Money.new("100", "USD")
iex> Logistiki.Accounting.Money.negate(m)
%Logistiki.Accounting.Money{amount: Decimal.new("-100"), currency: "USD"}
Builds a money value, coercing numeric inputs to Decimal.
Arguments
amount—Decimal.t() | integer() | String.t— the amount (e.g.Decimal.new("1000.00"),1000, or"1000.00").currency—String.t()oratom()— the currency code (e.g."USD"or:USD).
Returns
%__MODULE__{}— the money value.
Examples
iex> Logistiki.Accounting.Money.new(Decimal.new("100"), "USD")
%Logistiki.Accounting.Money{amount: Decimal.new("100"), currency: "USD"}
iex> Logistiki.Accounting.Money.new("50.00", "EUR")
%Logistiki.Accounting.Money{amount: Decimal.new("50.00"), currency: "EUR"}
iex> Logistiki.Accounting.Money.new(42, :USD)
%Logistiki.Accounting.Money{amount: Decimal.new(42), currency: "USD"}
True when the amount is greater than zero.
Arguments
m—%__MODULE__{}.
Returns
boolean().
Examples
iex> Logistiki.Accounting.Money.positive?(Logistiki.Accounting.Money.new("100", "USD"))
true
iex> Logistiki.Accounting.Money.positive?(Logistiki.Accounting.Money.zero("USD"))
false
Subtracts b from a (same currency).
Arguments
a—%__MODULE__{}.b—%__MODULE__{}— must have the samecurrency.
Returns
%__MODULE__{}—a.amount - b.amount.
Examples
iex> a = Logistiki.Accounting.Money.new("100", "USD")
iex> b = Logistiki.Accounting.Money.new("30", "USD")
iex> Logistiki.Accounting.Money.sub(a, b)
%Logistiki.Accounting.Money{amount: Decimal.new("70"), currency: "USD"}
Converts the amount to integer cents (rounded down).
Arguments
m—%__MODULE__{}.
Returns
integer()— the amount × 100, rounded down.
Examples
iex> Logistiki.Accounting.Money.to_cents(Logistiki.Accounting.Money.new("1000.00", "USD"))
100000
iex> Logistiki.Accounting.Money.to_cents(Logistiki.Accounting.Money.new("10.99", "USD"))
1099
Returns the zero amount for currency.
Arguments
currency—String.t()— e.g."USD".
Returns
%__MODULE__{amount: Decimal.new(0), currency: currency}.
Examples
iex> Logistiki.Accounting.Money.zero("USD")
%Logistiki.Accounting.Money{amount: Decimal.new(0), currency: "USD"}
True when the amount equals zero.
Arguments
m—%__MODULE__{}.
Returns
boolean().
Examples
iex> Logistiki.Accounting.Money.zero?(Logistiki.Accounting.Money.zero("USD"))
true