Expand description
Represents an amount of money in a specified currency denomination.
Money is an immutable value type for representing monetary amounts with an associated
currency. It supports both positive and negative values (for debits, losses, etc.) and
enforces currency consistency in arithmetic operations.
§Arithmetic behavior
Adding or subtracting two Money values requires matching effective fixed-point scales.
These operations panic on a scale mismatch.
Comparisons and hashes account for scale differences without rounding.
| Operation | Result | Notes |
|---|---|---|
Money + Money | Money | Panics if currencies don’t match. |
Money - Money | Money | Panics if currencies don’t match. |
Money + Decimal | Decimal | |
Money - Decimal | Decimal | |
Money * Decimal | Decimal | |
Money / Decimal | Decimal | |
Money + f64 | f64 | |
Money - f64 | f64 | |
Money * f64 | f64 | |
Money / f64 | f64 | |
-Money | Money |
§Ordering behavior
Rust ordering compares currency codes lexicographically, then scale-adjusted amounts. This order supports sorting and ordered collections across currencies, but it does not convert amounts to a common currency. Values with the same currency code compare by numeric value. Python comparisons reject values with different currency codes.
§Currency constraints
When performing arithmetic between two Money values, both must have the same currency.
Attempting to add or subtract money with different currencies raises an error.
§Immutability
Money is immutable. All arithmetic operations return new instances.
Structs§
- Money
- Represents an amount of money in a specified currency denomination.
Constants§
- MONEY_
MAX - The maximum valid money amount that can be represented.
- MONEY_
MIN - The minimum valid money amount that can be represented.
Statics§
- MONEY_
RAW_ MAX - The maximum raw money integer value.
- MONEY_
RAW_ MIN - The minimum raw money integer value.
Functions§
- check_
positive_ money - Checks if the money
valueis positive.