finanza/money

Amounts of money in a currency: arithmetic that refuses to mix currencies, allocation without losing a cent, and formatting.

The 15 currency constants (usd, eur, jpy, …) are a fixed snapshot of ISO 4217 taken in May 2026. Build a Currency record for anything else.

Types

A currency: its ISO 4217 code, the number of digits in its minor unit (2 for cents, 0 for yen), its symbol and its English name.

pub type Currency {
  Currency(
    code: String,
    exponent: Int,
    symbol: String,
    name: String,
  )
}

Constructors

  • Currency(
      code: String,
      exponent: Int,
      symbol: String,
      name: String,
    )

Options for format. Start from default_format and change the fields you need:

money.FormatOptions(..money.default_format, symbol_position: money.Suffix)
pub type FormatOptions {
  FormatOptions(
    symbol_position: SymbolPosition,
    thousands_separator: String,
    decimal_separator: String,
    negative_style: NegativeStyle,
    show_currency_code: Bool,
    round_to_minor_units: Bool,
  )
}

Constructors

  • FormatOptions(
      symbol_position: SymbolPosition,
      thousands_separator: String,
      decimal_separator: String,
      negative_style: NegativeStyle,
      show_currency_code: Bool,
      round_to_minor_units: Bool,
    )

    Arguments

    show_currency_code

    Append a space and the currency code, as in "$1.00 USD".

    round_to_minor_units

    Round the amount half-even to the currency’s minor unit, so dollars always have two places and yen none.

An amount in a currency. The amount may have more or fewer places than the currency’s minor unit; to_minor and format round it.

pub type Money {
  Money(amount: decimal.Decimal, currency: Currency)
}

Constructors

Why a money operation failed.

pub type MoneyError {
  CurrencyMismatch(left: String, right: String)
  EmptyRatios
  NonPositiveRatio
  ArithmeticFailed(decimal.ArithmeticError)
}

Constructors

  • CurrencyMismatch(left: String, right: String)

    The two amounts have different currency codes.

  • EmptyRatios

    allocate was given no ratios.

  • NonPositiveRatio

    allocate was given a negative ratio, or only zeros.

  • ArithmeticFailed(decimal.ArithmeticError)

    The decimal arithmetic underneath failed.

How format marks a negative amount.

pub type NegativeStyle {
  MinusSign
  Parentheses
}

Constructors

  • MinusSign
  • Parentheses

Where format puts the currency symbol.

pub type SymbolPosition {
  Prefix
  Suffix
  NoSymbol
}

Constructors

  • Prefix
  • Suffix
  • NoSymbol

Values

pub fn add(a: Money, b: Money) -> Result(Money, MoneyError)

Adds two amounts in the same currency.

Examples

let assert Ok(a) = money.from_minor(150, in: money.usd)
let assert Ok(b) = money.from_minor(1, in: money.eur)
money.add(a, b)
// -> Error(money.CurrencyMismatch(left: "USD", right: "EUR"))
pub fn allocate(
  money: Money,
  ratios: List(Int),
) -> Result(List(Money), MoneyError)

Splits the amount in proportion to ratios. The amount is first rounded half-even to the minor unit, and the minor units left over go one each to the first slots with a non-zero ratio, so the parts add up to the rounded amount. A zero ratio gets nothing.

Examples

let assert Ok(bill) = money.from_minor(1000, in: money.usd)
money.allocate(bill, [1, 1, 1])
|> result.map(list.map(_, money.to_string))
// -> Ok(["USD 3.34", "USD 3.33", "USD 3.33"])
money.allocate(bill, [0, 0])
// -> Error(money.NonPositiveRatio)
pub const aud: Currency

Australian Dollar.

pub const brl: Currency

Brazilian Real.

pub const cad: Currency

Canadian Dollar.

pub const chf: Currency

Swiss Franc.

pub const cny: Currency

Renminbi.

pub fn compare(
  a: Money,
  b: Money,
) -> Result(order.Order, MoneyError)

Compares two amounts in the same currency by value.

pub const default_format: FormatOptions

Formats as "-$1,234.56": symbol first, comma thousands, a dot before the cents, a minus sign, no code, and rounding to the minor unit.

pub fn divide(
  money: Money,
  by divisor: decimal.Decimal,
  rounding mode: decimal.RoundingMode,
) -> Result(Money, MoneyError)

Divides the amount by divisor and rounds the quotient to the currency’s minor unit. Use allocate to split a bill without losing the remainder.

pub const eur: Currency

Euro.

pub fn find_currency(code: String) -> Result(Currency, Nil)

Finds one of the 15 built-in currencies by its upper-case code.

Examples

money.find_currency("JPY")
// -> Ok(money.jpy)
money.find_currency("XYZ")
// -> Error(Nil)
pub fn format(
  money: Money,
  with options: FormatOptions,
) -> String

Renders the amount for display.

Examples

let assert Ok(price) = money.from_minor(-123_456, in: money.eur)
let options =
  money.FormatOptions(
    ..money.default_format,
    thousands_separator: ".",
    decimal_separator: ",",
    symbol_position: money.Suffix,
    negative_style: money.Parentheses,
  )
money.format(price, with: options)
// -> "(1.234,56€)"
pub fn from_major(
  amount: Int,
  in currency: Currency,
) -> Result(Money, MoneyError)

Creates money from a whole number of major units, so 35 is $35 and 3500 is ¥3,500.

pub fn from_minor(
  units: Int,
  in currency: Currency,
) -> Result(Money, MoneyError)

Creates money from a count of minor units, so 1234 cents is $12.34.

Returns Error(ArithmeticFailed(PrecisionExceeded)) when the count is outside ±9,007,199,254,740,991.

pub const gbp: Currency

Pound Sterling.

pub const hkd: Currency

Hong Kong Dollar.

pub const inr: Currency

Indian Rupee.

pub const jpy: Currency

Japanese Yen.

pub const krw: Currency

South Korean Won.

pub fn multiply(
  money: Money,
  by factor: decimal.Decimal,
  rounding mode: decimal.RoundingMode,
) -> Result(Money, MoneyError)

Multiplies the amount by factor and rounds the product to the currency’s minor unit.

Examples

let assert Ok(price) = money.from_minor(999, in: money.usd)
let assert Ok(rate) = decimal.parse("0.08")
money.multiply(price, by: rate, rounding: decimal.HalfEven)
|> result.map(money.to_string)
// -> Ok("USD 0.80")
pub const mxn: Currency

Mexican Peso.

pub fn negate(money: Money) -> Money

Returns the amount with its sign flipped.

pub const sgd: Currency

Singapore Dollar.

pub fn subtract(a: Money, b: Money) -> Result(Money, MoneyError)

Subtracts b from a. Both must be in the same currency.

pub fn sum(
  items: List(Money),
  in currency: Currency,
) -> Result(Money, MoneyError)

Adds up amounts that are all in currency. An empty list sums to zero.

pub fn to_minor(
  money: Money,
  rounding mode: decimal.RoundingMode,
) -> Result(Int, MoneyError)

Rounds the amount to the currency’s minor unit and returns the count of minor units, so $12.345 rounded half-even is 1234.

pub fn to_string(money: Money) -> String

Renders the code and the amount as stored, as in "USD 12.30".

pub const usd: Currency

US Dollar.

pub const zar: Currency

South African Rand.

✨ Search Document