From 2c2d92ef6b595aed9a03326474a0fcc7b008dd79 Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Fri, 9 Oct 2026 19:29:29 +0200 Subject: [PATCH] ROUND: keep 15 significant digits when asked for 15+ digits, so ROUND(0.1+0.2,15) is 0.3 (#58) --- crates/functions/src/util.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/functions/src/util.rs b/crates/functions/src/util.rs index fca5dc4..5296c48 100644 --- a/crates/functions/src/util.rs +++ b/crates/functions/src/util.rs @@ -235,7 +235,9 @@ pub(crate) fn round_half_away(x: f64, digits: i32) -> f64 { // value = 0.d1d2…d15 × 10^(e+1); keep the first `keep` digits. let keep = e + 1 + digits; if keep >= d.len() as i64 { - return x; + // Nothing to round off, but Excel still keeps only 15 significant digits, which drops binary noise + // such as 0.1 + 0.2 = 0.30000000000000004. + return to_sig_digits(x, d.len()); } if keep < 0 { return 0.0; @@ -416,6 +418,8 @@ mod tests { assert_eq!(round_half_away(1234.5678, -2), 1200.0); assert_eq!(round_half_away(0.285, 2), 0.29); assert_eq!(round_half_away(1.005, 2), 1.01); + assert_eq!(round_half_away(0.1 + 0.2, 15), 0.3); + assert_eq!(round_half_away(0.1 + 0.2, 20), 0.3); } #[test]