Tests encode the display rules: half away from zero, no negative zero, no percent of a zero or negative base, plus a scoped source check that no round_dp or float conversion remains in the money display paths. view_model's pct_change(-100, -50) expectation moves from Some(50.0) to None under the new rule. docs/proofs/display.md is the unit note (no Kani, no Lean for display, per the audit). Co-Authored-By: Muse Wonder 1.0
6.2 KiB
K-U9: display rounding, and percentages in Decimal
Folded into docs/verification.md by the integrator; this note is the
unit's own record.
Kernel sweep unit U9 (plan/kernel-sweep-audit.md section 3, "display
rounding"). Display only: no kernel function, no Kani harness, no Lean
theorem (the audit and docs/proofs/README.md say so for U9). The
rounding it shares is the kernel's already-proved round_places
(K-U2, law 40); the percentages it shares are kernel::percent_of
(K-U6).
The API
// ledger-import::format (re-exported at the crate root)
pub fn round_display(amount: Decimal, places: u32) -> Decimal
Half away from zero to places, through
ledger_core::kernel::round_places(..., Rounding::HalfAwayFromZero),
never a negative zero. It is the rounding half of
ledger-import::report::format_amount, lifted out so every renderer
shares it: format_amount and amount_parts call it at two places,
ledger-import::pdf's percent column at one place,
ledger-import::xlsx::exact_f64 at two places (before its exactness
check), ledger-package::text's money/share_text at the amount's
own scale (the identity there: padding only, never rounding, as those
functions promise), and ledger-gui::fmt re-exports it for the pages.
The percent rule (one rule for every percent)
A percent of a zero or negative whole is None, and the page shows
nothing (a KPI tile keeps no sub-line) or "—" (a table cell). That is
kernel::percent_of's rule, and K-U9 adopts it everywhere:
ledger-gui::fmt::pct_changereturnsOption<Decimal>(it wasOption<f64>, dividing money amounts in binary floating point):(now - prev) / prev * 100to one place, half away from zero, viapercent_of. A negativeprevgave a signed percent before; nowNone.ledger-gui::pages::profit::pct(margins, shares of invoiced income):percent_ofat one place, half to even — the books' margin mode, ascontribution_margin_pctuses. A negative revenue base gave a percent before; nowNone, shown as "—".ledger-gui::pages::reports' change column: wasdiff / base.abs() * 100, a percent of a negative base with the sign hidden; nowpercent_of(diff, base, ...),Nonefor base <= 0. The "% of income" column likewise adds no column when total income is zero or negative (it added none for zero before).ledger-gui::pages::capital::percent: a member's share is a stored fraction, not a ratio of two amounts, so it stays exact decimal multiplication by 100 with no rounding; only a negative zero is dropped. No float was involved.
Sites routed
ledger-import:report::format_amount,report::amount_parts,pdfpercent column,xlsx::exact_f64— throughround_display.ledger-package::text::money,share_text— throughround_displayat their own scale (identity; see above).ledger-gui::fmt::compact: wasf64(try_into,{:.1}prints). Now decimal division by 1,000 / 1,000,000 andround_display. Two displayed figures change, both f64 artefacts: 1,250,000 shows "$1.3M" (was "$1.2M": 1.25 in binary is slightly under), 10,500 shows "$11k" (was "$10k": float formatting rounds 10.5 to even).ledger-gui::fmt::pct_changeand its callerpages/overview::change("▲ 12% vs …" text and the 0.5% colour threshold): Decimal throughout.pages/overviewrunway months,pages/cashaverage net and runway,pages/profitspending row,pages/reportschange / share / TSV,booksmonthly-burn average,demoplatform fee:round_dp(1|2)throughround_display(checked division where the old code divided raw under a guard).books' two share splits truncated to cents withToZero: that is a leftover policy, not display rounding, so it now goes throughkernel::round_places(..., TowardZero)astruncate_cents, with no change of value.
Floats kept, with why
ledger-gui::charts(f(Decimal) -> f64,ticks, sparklines,pages/overview/cashspark vectors,widgets::Kpi::spark): pixel coordinates and axis scales computed for layout only. Every hover and label that states a figure reads the originalDecimal(fmt::dollars,fmt::compact); no printed amount is computed in a float.- egui layout
f32s (column widths, row heights), colour maths inschemes.rs, image sizes inreceipts.rs: geometry and colour, never money. - Time
f64s (app::since_update,widgetstimers,pages/reports::pdf_done,pages/transactions::since_update): egui's clock, never money. fmt::bytes(n as f64): a byte count for "12.3 KB", not money.ledger-import::labelsgeometry (f64millimetre/point layout),asset_register's life-in-months cell (f64::from(months): a count),xlsxentry-id cells (as f64: an id): layout and counts.ledger-import::xlsx::exact_f64(to_f64): the one sanctioned conversion — an xlsx cell is a binary double, and a value is written as a number only when it is whole cents of at most 15 significant digits, exactly representable; anything else is text.ledger-mcp::typesvisit_f64: serde's visitor signature for refusing a float TIN, not a computation.ledger-import::templates::equal_shares: owner-percentage template (audit class (c)), last-absorbs so the shares sum to 100; not a display rounding of a money amount, left as is.
Tests
crates/ledger-import/src/format.rs: half away from zero at ties, never a negative zero.ledger-gui::fmttests:pct_changedecimal-or-none (zero and negative bases),compactdecimal ties.ledger-gui::pages::profittests: no percent of a negative base.ledger-gui::pages::reportstests: the change column has no percent from a zero or negative base; no "% of income" column for negative income.crates/ledger-gui/tests/display_rounding.rs: the rules through the public API, plus a scoped source check (noround_dpin the display files; nof64infmt's money half;to_f64only inxlsx.rs).
No golden changes. Displayed values that changed are the two
compact ties above and the percents of negative bases (now "—" /
no sub-line instead of a signed figure). One existing test encoded
the old negative-base percent (ledger-gui/tests/view_model.rs:
pct_change(-100, -50) was Some(50.0)); it now expects None,
per the rule above.