Skip to main content

Module money

Module money 

Source
Expand description

Money — an exact monetary amount in a specific currency (UNIVERSAL_TYPES Part V).

Money rides the exact Decimal tower, so it never float-drifts: 0.10 + 0.20 is exactly 0.30, not 0.30000000000000004. The amount is quantised to the currency’s minor unit (USD has 2 fractional digits, JPY has 0, BHD has 3), with banker’s rounding.

Currency is part of the value, and same-currency arithmetic is the only kind that means anything: 5 USD + 1 EUR has no answer, so add/sub across currencies return None — the exact analogue of the dimensional rule that forbids meter + gram. × ÷ by a plain number scales the amount; a same-currency Money ÷ Money is an exact dimensionless ratio.

Modules§

currency
The currency catalog — resolve an ISO-4217 code (case-insensitive) to its Currency. A growable data table, mirroring crate::quantity::units::by_name; the minor-unit scales follow ISO-4217.

Structs§

Currency
An ISO-4217 currency: its three-letter code and its minor-unit scale (the number of fractional digits — USD→2, JPY→0, BHD→3). The code is the identity.
Money
An exact monetary amount, quantised to its currency’s minor unit.
RateTable
A pluggable exchange-rate table — the substrate of the Universal Money Amount (money’s UTC). Each currency code maps to its rate versus a chosen reference currency: how many reference units one unit of the currency is worth, exact on the Rational tower (the reference itself is 1). The source is interchangeable — a literal table, a CRDT-synced table, an API fetch — they all produce this same value, so with rates in scope money of different currencies shares one coordinate and cross-currency math becomes meaningful (and exact).

Functions§

ambient_convert
Convert m to to using the ambient rate table. None if no rates are in scope or a currency’s rate is missing — the caller surfaces a clean error rather than a wrong number.
clear_ambient_rates
Drop the ambient rate table (back to “no rates in scope”).
has_ambient_rates
True if any rate context is in scope.
set_ambient_rate
Add or replace one rate in the ambient table (1 <code> = rate reference units), creating the table if none is in scope yet.
set_ambient_rates
Install (replace) the ambient rate table.