Expand description
Display context for formatting numbers with consistent precision.
This module provides the DisplayContext type which tracks a frequency
distribution of decimal places per currency, observed during parsing. The
configured Precision policy then determines how that distribution is
collapsed to a single per-currency precision for display.
Default policy is Precision::MostCommon — the mode of the dp
distribution. This matches Python bean-query’s default rendering and
ensures that outliers (e.g. a single 28-decimal computed price annotation)
don’t inflate the display precision for an otherwise 2dp-dominant currency.
Precision::Maximum selects the highest dp ever observed, which is what
Python uses when rendering price tables. Callers opt in via
DisplayContext::set_precision.
§Example
use rustledger_core::DisplayContext;
use rust_decimal_macros::dec;
let mut ctx = DisplayContext::new();
// Track samples for USD: tied 1×0dp + 1×2dp → tie-break favors larger.
ctx.update(dec!(100), "USD"); // 0 dp
ctx.update(dec!(50.25), "USD"); // 2 dp
ctx.update(dec!(1.5), "EUR"); // 1 dp
// Default policy (MostCommon) returns the mode of the per-currency dist.
assert_eq!(ctx.get_precision("USD"), Some(2));
assert_eq!(ctx.get_precision("EUR"), Some(1));
assert_eq!(ctx.get_precision("GBP"), None); // Never seen
// format() uses the policy's effective precision.
assert_eq!(ctx.format(dec!(100), "USD"), "100.00");
assert_eq!(ctx.format(dec!(50.25), "USD"), "50.25");
assert_eq!(ctx.format(dec!(1.5), "EUR"), "1.5");Structs§
- Display
Context - Display context for formatting numbers with consistent precision per currency.
Enums§
- Precision
- Policy for resolving the per-currency display precision from the observed distribution.
Constants§
- DEFAULT_
CURRENCY - Sentinel currency key for “naked-decimal” observations.