From 9f7d1c426f5bf1f193b8120ab70744b0175a2f5a Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:44:43 +0100 Subject: [PATCH] fix: round engineering notation after forming the mantissa (#2148) Keep chart labels such as 999.94 in ##0.0E+00 at 999.9E+00 until the full mantissa actually rounds past its available digits. Add regression coverage for widths, signs, small values, carries and decimal localization. --- crates/model/src/numfmt.rs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/crates/model/src/numfmt.rs b/crates/model/src/numfmt.rs index b5db9667c..7a7b86665 100644 --- a/crates/model/src/numfmt.rs +++ b/crates/model/src/numfmt.rs @@ -234,16 +234,18 @@ fn sci(toks: &[Tok], v: f64, sep: char, dec: char) -> Option { m *= 10.0; exp -= 1; } - // Rounding the mantissa to its digits can reach 10 (9.999 as `0.00`). - if format!("{m:.mant_frac$}").starts_with("10") { - m /= 10.0; - exp += 1; - } } let step = mant.iter().take_while(|t| **t != Tok::Dot).filter(|t| matches!(t, Tok::Digit(_))).count().max(1) as i32; let shift = exp.rem_euclid(step); exp -= shift; m *= 10f64.powi(shift); + // Round after forming the engineering mantissa: 999.94 as `##0.0` stays 999.9. + // Carry only when rounding exceeds all of the format's integer placeholders. + let carry = 10f64.powi(step); + if format!("{m:.mant_frac$}").parse::().is_ok_and(|rounded| rounded >= carry) { + m /= carry; + exp += step; + } let mantissa = fixed(mant, mfirst, mlast, m, sep, dec); let sign = if exp < 0 { "-" @@ -404,6 +406,21 @@ mod tests { assert_eq!(format("##0.0E+0", 999.96, None), "1.0E+3"); } + #[test] + fn engineering_formats_round_the_full_mantissa() { + for (code, value, expected) in [ + ("##0.0E+00", 999.94, "999.9E+00"), + ("##0.0E+00", 0.99994, "999.9E-03"), + ("##0.0E+00", -999.94, "-999.9E+00"), + ("#0.0E+00", 99.94, "99.9E+00"), + ("##0.0E+00", 999.96, "1.0E+03"), + ("#0.0E+00", 99.96, "1.0E+02"), + ] { + assert_eq!(format(code, value, None), expected, "{code}, {value}"); + } + assert_eq!(format("##0.0E+00", 999.94, Some("de-DE")), "999,9E+00"); + } + #[test] fn never_panics() { for code in ["", ";", ";;;", "\"", "[", "\\", "0.", ".", "#,", "%", "_", "*", "@", "0E+00", "[$", "\"unterminated"] {