merge: one rounding for display, percentages in Decimal (K-U9; checked, one fix)
Three crates each rounded an amount for display in their own way, and
the viewer computed its percentages in f64. Money is never a float, not
for a ratio either (AGENTS.md's first rule), and a figure shown should
not depend on which renderer drew it.
- `ledger_import::format::round_display(amount, places)`: half away
from zero through the kernel's `round_places`, never a negative zero.
The reports' text, the PDFs, the spreadsheet, the CPA package's text
and the viewer's formatting all call it.
- The viewer's percentages (`fmt::pct_change`, the Profit page, the
reports' change and share-of-income columns) are the kernel's
`percent_of` on Decimals.
What a reader sees differently, by principle and not by accident:
- `compact` rounds half away from zero like every other figure:
1,250,000 reads $1.3M (was $1.2M), 10,500 reads $11k (was $10k).
- A change against a base that is zero or negative shows no percentage
("—"), where it used to show one with a misleading sign: from a loss
of 100 to a loss of 50 is not "50% down".
No booked figure moves and no golden changed. One fix on merge: the
demo books' platform fee is a booked amount, so it uses the kernel's
rounding with the rule it always had (a half to the even cent), not the
display rounding the branch had put there; the demo books are unchanged.
Shown on main: clippy clean on the three crates; ledger-import's unit
tests 44 passed and spec_import, labels, templates 91 passed;
ledger-package 49 passed; the viewer's unit tests 62 passed, and
display_rounding, view_model, sidenav, spec_gui, odd_cent 59 passed;
ledger-core's bookspec checks 78 passed (the worker's one failure there
was its old base, which lacked the pin at bookspec 79cb439).
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
commit
0e9990f111
17 files changed
+506
-62
No files matched your search
@@ -22,7 +22,10 @@ use rust_decimal::Decimal;
|
||||
/// "318.42", "1000.00". Padding only, never rounding: an exact amount with
|
||||
/// more places (an FX-converted line) keeps them. Never "-0".
|
||||
pub fn money(n: Decimal) -> String {
|
||||
let mut n = if n.is_zero() { Decimal::ZERO } else { n };
|
||||
// Padding only, never rounding: the display rounding at the
|
||||
// amount's own scale (at least two places) is the identity, and it
|
||||
// is what drops a negative zero.
|
||||
let mut n = ledger_import::round_display(n, n.scale().max(2));
|
||||
if n.scale() < 2 {
|
||||
n.rescale(2);
|
||||
}
|
||||
@@ -42,6 +45,7 @@ pub fn total_text(total: Option<Decimal>) -> String {
|
||||
/// share, never on its spelling.
|
||||
pub fn share_text(n: Decimal) -> String {
|
||||
let mut n = n.normalize();
|
||||
n = ledger_import::round_display(n, n.scale().max(2));
|
||||
if n.scale() < 2 {
|
||||
n.rescale(2);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user