An amount of money in one currency, as a value object.
field :price, :money # shorthand for {:one, DDD.Money}
{:ok, price} = DDD.Money.of("19.99", "EUR")
{:ok, total} = DDD.Money.add(price, price)
{:ok, half} = DDD.Money.multiply(price, "0.5")The amount is a Decimal, never a float. The currency is a 3-letter ISO 4217 code.
Adding or subtracting two currencies is a business error (:currency_mismatch); there is no
silent conversion. Money is stored as jsonb ({"amount": "19.99", "currency": "EUR"}).
Summary
Functions
Sum of two amounts in the same currency.
Compare two amounts of the same currency: {:ok, :lt | :eq | :gt}.
Multiply by a number (Decimal, integer, float or string).
True for an amount below zero.
Build money from an amount (Decimal, integer, float or string) and a currency.
Like of/2 but returns the money or raises.
True for an amount above zero.
Round to places decimal places (default 2), half up.
Difference of two amounts in the same currency (may be negative).
Run field rules, then specification invariants. Returns the sealed
struct or a DDD.Error listing every violation.
Zero in currency.
True for an amount of exactly zero.
Functions
Sum of two amounts in the same currency.
iex> {:ok, sum} = DDD.Money.add(DDD.Money.of!("1.10", "EUR"), DDD.Money.of!("2.20", "EUR"))
iex> Decimal.to_string(sum.amount)
"3.30"
iex> {:error, error} = DDD.Money.add(DDD.Money.of!(1, "EUR"), DDD.Money.of!(1, "USD"))
iex> error.code
:currency_mismatch
Compare two amounts of the same currency: {:ok, :lt | :eq | :gt}.
Multiply by a number (Decimal, integer, float or string).
True for an amount below zero.
@spec of(Decimal.t() | integer() | float() | String.t(), String.t()) :: {:ok, struct()} | {:error, DDD.Error.t()}
Build money from an amount (Decimal, integer, float or string) and a currency.
Like of/2 but returns the money or raises.
True for an amount above zero.
Round to places decimal places (default 2), half up.
Difference of two amounts in the same currency (may be negative).
Run field rules, then specification invariants. Returns the sealed
struct or a DDD.Error listing every violation.
Zero in currency.
True for an amount of exactly zero.