🔗Fixed-point money
Amount<P> is an i64 count of 10⁻ᴾ, with four sign-symmetric rounding modes and typed Percents. Totals come out at the document’s scale (97.47, or cents). Overflow never wraps.
Rust · no_std · zero I/O · no unsafe
billing computes the numbers on an EN 16931 invoice — line nets, allowances and charges, the VAT breakdown, the totals chain, payments and cash rounding. You state the inputs; every total is derived once, at the document’s scale.
Get started API reference GitHub
cargo add billing
EN 16931 caps every amount at two decimals and requires the totals to reconcile exactly at that precision. Rounding at the serialiser breaks that (BR-CO-10, BR-CO-15). billing rounds each leaf once and derives every total from the rounded leaves.
use billing::{Amount, AmountScale, Currency, Document, Line, Percent, Price, Quantity, UnitPrice, Vat};
use rust_decimal::dec;
// 1234.567 kWh × 0.28901 EUR/kWh = 356.80221 — five decimals.
let doc = Document::builder(Currency::EUR, AmountScale::EN16931)
.default_vat(Vat::standard(Percent::new(dec!(19)))?)
.line(Line::new(
"Arbeit",
Quantity::new(dec!(1234.567), "KWH"),
UnitPrice::new(Price::new(dec!(0.28901))),
))
.build()?;
assert_eq!(doc.line_net(0), Amount::parse("356.80")?); // BT-131, rounded once
assert_eq!(doc.vat_total(), Amount::parse("67.79")?); // BT-110: 356.80 × 19 %
assert_eq!(doc.payable(), Amount::parse("424.59")?); // BT-115Amount<P> is an i64 count of 10⁻ᴾ, with four sign-symmetric rounding modes and typed Percents. Totals come out at the document’s scale (97.47, or cents). Overflow never wraps.
Ten aggregations with exact averages; per-unit, graduated, volume and package tariffs; spot series, a tariff per key, transaction fees, progressive billing and 95th percentiles.
Every line and adjustment carries a Vat; the breakdown (BG-23) is a group-by with the tax rounded once per group.
split_gross turns a shelf price into net and tax once; the halves re-add exactly.
Largest remainder in integer arithmetic, down to Document::split. No cent is created or lost.
negated, concat, minimum and maximum bounds, advances, customer credits, cash rounding, Skonto and late-payment interest.
A Document exists only through build(); every accessor is infallible and there is no validate().
Every error says why, where and which EN 16931 or Peppol rule it enforces; each rule id is hash-locked against the pinned Schematron.
Property tests state the algebraic laws, fuzzers attack the parser, splitter and serde, and cargo-mutants checks the suite.
A library of arithmetic — not a billing platform, not an invoice model. Invoice numbers, dates, parties, type codes and XML belong to en16931 and en16931-formats.