refactor(core): one rounding primitive, round_step, in the kernel

Every money round_dp*/rescale in ledger-core now goes through
kernel::round_step: amount to a whole multiple of a step, in a named mode
(half away, half even, toward zero, toward negative infinity), exact
integer arithmetic on Decimal mantissas and scales, None instead of a
panic, no negative zero. The call sites use round_places, which reproduces
Decimal::round_dp_with_strategy exactly (value, scale and its early
return), and add_line_fx uses round_minor for the base currency's smallest
unit. This is a refactor: every site produces exactly what it produced
before, so no golden changes.

The schedules' monthly-even plan keeps its two-place cent rule: moving it
to the currency's minor unit is K-U5's decision (D-72), not this
behaviour-preserving unit. The remaining rescales (money.rs's display
padding, the contribution margin's one place) are formatting, not
rounding, and stay.

Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai>
This commit is contained in:
swaitsandDeepSeek V4.1 Flash committed 2026-11-08 13:37:48 -07:00
1 parent 75c666a7ce
commit 74943fb699
12 files changed
+375 -41

No files matched your search

+7 -5
View File
@@ -456,7 +456,7 @@ impl RunningSplit {
whole: Decimal,
weights: &[Decimal],
) -> Result<Vec<Decimal>, LedgerError> {
use rust_decimal::RoundingStrategy::ToNegativeInfinity;
use crate::kernel::{Rounding, round_places};
let n = self.given.len();
let Some(sum) = self.weight_sum(weights)? else {
return Ok(vec![Decimal::ZERO; n]);
@@ -482,12 +482,14 @@ impl RunningSplit {
// rounded down to the cent, what rounding took its remainder.
let snapped: Vec<Decimal> = exact
.iter()
.map(|x| (x * sign).round_dp(EXACT_PLACES))
.collect();
.map(|x| round_places(x * sign, EXACT_PLACES, Rounding::HalfToEven))
.collect::<Option<_>>()
.ok_or_else(too_large)?;
let mut parts: Vec<Decimal> = snapped
.iter()
.map(|x| x.round_dp_with_strategy(2, ToNegativeInfinity))
.collect();
.map(|x| round_places(*x, 2, Rounding::TowardNegativeInfinity))
.collect::<Option<_>>()
.ok_or_else(too_large)?;
let remainders: Vec<Decimal> = snapped.iter().zip(&parts).map(|(x, p)| x - p).collect();
let given: Vec<Decimal> = self.given.iter().map(|g| g * sign).collect();
crate::kernel::apportion_cumulative(