Logistiki.Accounting.Money (logistiki v0.1.0)

Copy Markdown View Source

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

  • amountDecimal.t() — the monetary amount
  • currencyString.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

Types

t()

The struct type. See the module documentation for field details and examples.

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

t()

@type t() :: %Logistiki.Accounting.Money{amount: Decimal.t(), currency: String.t()}

The struct type. See the module documentation for field details and examples.

Functions

add(money1, money2)

(since 0.1.0)
@spec add(t(), t()) :: t()

Adds two money values of the same currency.

Arguments

  • a%__MODULE__{}.
  • b%__MODULE__{} — must have the same currency as a.

Returns

  • %__MODULE__{}a.amount + b.amount in 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"}

negate(money)

(since 0.1.0)
@spec negate(t()) :: t()

Negates the amount.

Arguments

  • m%__MODULE__{}.

Returns

  • %__MODULE__{} — with amount negated.

Examples

iex> m = Logistiki.Accounting.Money.new("100", "USD")
iex> Logistiki.Accounting.Money.negate(m)
%Logistiki.Accounting.Money{amount: Decimal.new("-100"), currency: "USD"}

new(amount, currency)

(since 0.1.0)
@spec new(Decimal.t() | integer() | String.t(), String.t() | atom()) :: t()

Builds a money value, coercing numeric inputs to Decimal.

Arguments

  • amountDecimal.t() | integer() | String.t — the amount (e.g. Decimal.new("1000.00"), 1000, or "1000.00").

  • currencyString.t() or atom() — 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"}

positive?(money)

(since 0.1.0)
@spec positive?(t()) :: boolean()

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

sub(money1, money2)

(since 0.1.0)
@spec sub(t(), t()) :: t()

Subtracts b from a (same currency).

Arguments

  • a%__MODULE__{}.
  • b%__MODULE__{} — must have the same currency.

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"}

to_cents(money)

(since 0.1.0)
@spec to_cents(t()) :: integer()

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

zero(currency)

(since 0.1.0)
@spec zero(String.t()) :: t()

Returns the zero amount for currency.

Arguments

  • currencyString.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"}

zero?(money)

(since 0.1.0)
@spec zero?(t()) :: boolean()

True when the amount equals zero.

Arguments

  • m%__MODULE__{}.

Returns

  • boolean().

Examples

iex> Logistiki.Accounting.Money.zero?(Logistiki.Accounting.Money.zero("USD"))
true