rs-math-money 0.1.0
Fixed-point decimal money in integer minor units with ISO 4217 currencies, rounding modes, lossless allocate and split, checked math and parse/format, std only
Install
cargo add rs-math-money --registry rupm
In Ruststudio: Packages, search for rs-math-money, Install.
Owner: Rust Studio. Licence: MIT OR Apache-2.0. Total downloads: 0. Published 2026-11-23.
Versions
- 0.1.0 2026-11-23
README
# rs-math-money
Fixed-point decimal money in integer minor units with ISO 4217 currencies, rounding modes, lossless allocate and split, checked math and parse/format, std only
Add it with `rupm add rs-math-money` (or `cargo add rs-math-money --registry rupm`).
Amounts are stored as whole minor units (cents, pence, yen, fils) in an `i128`, so there is no floating point anywhere and no drift. The number of decimals comes from the currency (USD 2, JPY 0, KWD 3). Every operation that can overflow, mix currencies or divide by zero returns a `Result` instead of panicking. The rounding modes are checked against the table in the Java `RoundingMode` documentation, and allocation follows the largest-remainder rule so shares always add up to the original amount.
## Install
```
rupm add rs-math-money
```
## Usage
```rust
use rs_math_money::{Currency, Money, RoundingMode};
let usd = Currency::new("USD").unwrap();
let price: Money = "19.99 USD".parse().unwrap();
assert_eq!(price.checked_add(Money::from_minor(1, usd)).unwrap().to_string(), "20.00 USD");
// Sales tax of 8.25%, rounded half up.
let tax = price.mul_decimal("0.0825", RoundingMode::HalfUp).unwrap();
assert_eq!(tax.to_string(), "1.65 USD");
// Split $1.00 three ways without losing a cent.
let parts = Money::from_minor(100, usd).split(3).unwrap();
let cents: Vec<i128> = parts.iter().map(|m| m.minor()).collect();
assert_eq!(cents, vec![34, 33, 33]);
// Weighted allocation.
let shares = Money::from_minor(5, usd).allocate(&[3, 7]).unwrap();
assert_eq!((shares[0].minor(), shares[1].minor()), (2, 3));
assert_eq!(Money::from_minor(123456789, usd).to_string_grouped(), "1,234,567.89 USD");
```
## API overview
- `Currency::new("EUR")`: validated ISO 4217 code (case-insensitive) with `code()` and `exponent()`.
- `Money::from_minor`, `from_major`, `zero`, `minor()`, `currency()`, `is_zero`, `is_negative`, `abs`.
- Checked math: `checked_add`, `checked_sub`, `checked_neg`, `checked_mul_int`, `div_int(n, mode)`, `mul_ratio(num, den, mode)`, `mul_decimal("0.0825", mode)`. Comparison with `PartialOrd` (different currencies are unordered).
- `RoundingMode`: `Down`, `Up`, `Floor`, `Ceiling`, `HalfUp`, `HalfDown`, `HalfEven`; `round_div(n, d, mode)` rounds an integer division.
- `allocate(&[ratios])` and `split(n)`: distribute an amount, leftover minor units go to the first shares.
- Parsing: `"12.34 USD"`, `"USD 12.34"`, `"-5 JPY"` via `FromStr` (too many decimals is an error), `Money::parse_rounded(text, currency, mode)` for amounts without a code.
- Formatting: `Display` gives `12.34 USD`; `amount_string()` gives `12.34`; `to_string_grouped()` adds thousands separators.
- `MoneyError`: `UnknownCurrency`, `CurrencyMismatch`, `Overflow`, `DivisionByZero`, `InvalidFormat`, `TooPrecise`, `InvalidRatio`.