refactor(import): Decimal arithmetic goes through the kernel (K-U12)

The raw-decimal-arith detector counted 18 places in ledger-import before
and counts 0 after. No figure changes.

Signs (bank_csv, iif, invoice_pdf, report) go through money::neg and
money::signed_amount; sums and differences of figures from the books
(report, xlsx) through total_of, add_in_range and sub_in_range, which
are exact and panic only where the raw operator panicked.

Not a plain sum or sign: equal_shares (templates) now divides through
Amount::checked_div, rounds half-to-even to two places through
round_places, and gives the last owner a hundred minus the shares
given. Same numbers as before for every n >= 1; for n = 0 it returns no
shares where the raw division panicked (its one caller passes the
owners of a partnership, at least two).

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude committed 2026-11-09 23:55:32 -07:00
1 parent 0de906e9ed
commit 0fb19b4b25
6 files changed
+43 -20

No files matched your search

+9 -2
View File
@@ -115,10 +115,17 @@ fn acct(code: &str, name: &str, t: &str, tax: Option<&str>) -> ChartRow {
/// Equal shares for `n` owners as percents to two places, the last
/// absorbing the remainder so they sum to exactly 100.
fn equal_shares(n: usize) -> Vec<Decimal> {
let each = (Decimal::from(100) / Decimal::from(n as u64)).round_dp(2);
use ledger_core::kernel::{Amount, Rounding, round_places, total};
let hundred = Decimal::from(100);
let Some(each) = Amount::checked_div(hundred, Decimal::from(n as u64))
.and_then(|q| round_places(q, 2, Rounding::HalfToEven))
else {
return Vec::new(); // no owners: no shares (the raw `/` panicked here)
};
let mut v = vec![each; n];
if let Some((last, rest)) = v.split_last_mut() {
*last = Decimal::from(100) - rest.iter().sum::<Decimal>();
let given = total(rest.iter().copied()).expect("fewer than a hundred percent, n times");
*last = Amount::checked_sub(hundred, given).expect("a hundred minus the shares given fits");
}
v
}