gtfs/common/currency

Currency Handling Utilities

GTFS uses ISO 4217 currency codes and handles monetary amounts. This module provides parsing and formatting utilities with proper decimal handling to avoid floating-point precision issues.

Source: GTFS reference.md - Field Types

Values

pub fn add(
  a: types.CurrencyAmount,
  b: types.CurrencyAmount,
) -> types.CurrencyAmount

Add two currency amounts (normalizes to higher precision)

pub fn amount_from_float(
  value: Float,
  decimal_places: Int,
) -> types.CurrencyAmount

Create a CurrencyAmount from a Float (convenience function) Note: For precise amounts, use parse_amount instead

pub fn amount_to_float(amount: types.CurrencyAmount) -> Float

Convert amount to a Float (for display purposes only, not calculations)

pub fn amount_to_string(amount: types.CurrencyAmount) -> String

Format a currency amount as a decimal string Example: CurrencyAmount(250, 2) -> “2.50”

pub fn aud() -> types.CurrencyCode

Australian Dollar

pub fn cad() -> types.CurrencyCode

Canadian Dollar

pub fn compare(
  a: types.CurrencyAmount,
  b: types.CurrencyAmount,
) -> order.Order

Compare two currency amounts

pub fn currency_decimal_places(
  currency: types.CurrencyCode,
) -> Int

Get the number of decimal places typically used for a currency

pub fn currency_symbol(currency: types.CurrencyCode) -> String

Get the symbol for common currencies

pub fn eur() -> types.CurrencyCode

Euro

pub fn format(
  amount: types.CurrencyAmount,
  currency: types.CurrencyCode,
) -> String

Format a currency amount with its currency code Example: CurrencyAmount(250, 2), CurrencyCode(“USD”) -> “USD 2.50”

pub fn format_with_symbol(
  amount: types.CurrencyAmount,
  currency: types.CurrencyCode,
) -> String

Format with symbol (for common currencies)

pub fn gbp() -> types.CurrencyCode

British Pound

pub fn is_negative(amount: types.CurrencyAmount) -> Bool

Check if amount is negative

pub fn is_positive(amount: types.CurrencyAmount) -> Bool

Check if amount is positive

pub fn is_zero(amount: types.CurrencyAmount) -> Bool

Check if amount is zero

pub fn jpy() -> types.CurrencyCode

Japanese Yen

pub fn multiply(
  amount: types.CurrencyAmount,
  factor: Int,
) -> types.CurrencyAmount

Multiply a currency amount by an integer

pub fn parse_amount(
  value: String,
) -> Result(types.CurrencyAmount, String)

Parse a currency amount from a string Handles decimal amounts properly using integer arithmetic Examples: “2.50” -> CurrencyAmount(250, 2), “100” -> CurrencyAmount(100, 0)

pub fn parse_amount_optional(
  value: String,
) -> Result(option.Option(types.CurrencyAmount), String)

Parse an optional currency amount

pub fn parse_code(
  value: String,
) -> Result(types.CurrencyCode, String)

Parse an ISO 4217 currency code (3-letter code)

pub fn parse_code_optional(
  value: String,
) -> Result(option.Option(types.CurrencyCode), String)

Parse an optional currency code

pub fn subtract(
  a: types.CurrencyAmount,
  b: types.CurrencyAmount,
) -> types.CurrencyAmount

Subtract two currency amounts (normalizes to higher precision)

pub fn usd() -> types.CurrencyCode

US Dollar

pub fn zero(decimal_places: Int) -> types.CurrencyAmount

Zero amount with specified decimal places

Search Document