money/money

This module provides types and methods for safe monetary calculations

Types

Monetary value that consists of a amount and its currency

pub opaque type Money

Represents the error when trying to add two currencies that don’t match

pub type MoneyError {
  CurrencyMismatch(
    expected_currency: String,
    received_currency: String,
  )
}

Constructors

  • CurrencyMismatch(
      expected_currency: String,
      received_currency: String,
    )

    Currencies that failed to match

Values

pub fn add(
  from: Money,
  other: Money,
) -> Result(Money, MoneyError)

Safely add two amounts of money and produce the new amount or, an error otherwise

pub fn new(amount: Int, currency: String) -> Money

Produces a new Money ADT

pub fn to_string(m: Money) -> String

Produce the m amount and its currency as a string

Search Document