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

+16
View File
@@ -2,6 +2,7 @@
//! grouping the lines of an entry or a book, unchanged in U0. The laws it
//! serves are listed at the module root, [`super`].
use super::round::Rounding;
use crate::assets::BasisRule;
use crate::{AccountType, Side};
use rust_decimal::Decimal;
@@ -41,6 +42,12 @@ pub trait Amount: Copy + Ord {
/// B-21), whose shares are added exactly and rounded once, never
/// share by share.
fn checked_div_round_cent(self, rhs: Self) -> Option<Self>;
/// `self` rounded to a whole multiple of `step`, in `mode`; `None`
/// when `step` is not positive or the result cannot be represented.
///
/// The instantiation of [`crate::kernel::round_step`], which the call
/// sites use; a machine integer is already a whole smallest unit.
fn checked_round_to_step(self, step: Self, mode: Rounding) -> Option<Self>;
/// `self - rhs`, or `None` when it cannot be represented exactly.
fn checked_sub(self, rhs: Self) -> Option<Self> {
@@ -148,6 +155,9 @@ impl Amount for Decimal {
let signed = if negative { -signed_mag } else { signed_mag };
Decimal::try_from_i128_with_scale(signed, scale).ok()
}
fn checked_round_to_step(self, step: Self, mode: Rounding) -> Option<Self> {
super::round::round_decimal_to_step(self, step, mode)
}
}
impl Amount for i32 {
@@ -196,6 +206,9 @@ impl Amount for i32 {
q.checked_sub(1)
}
}
fn checked_round_to_step(self, step: Self, mode: Rounding) -> Option<Self> {
super::round::round_i32_to_step(self, step, mode)
}
}
impl Amount for i64 {
@@ -245,6 +258,9 @@ impl Amount for i64 {
q.checked_sub(1)
}
}
fn checked_round_to_step(self, step: Self, mode: Rounding) -> Option<Self> {
super::round::round_i64_to_step(self, step, mode)
}
}
/// Why the kernel rejected an entry's lines.