From bd8cf4e14a65df80f80167a5f3bbbc7ce49147af Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Fri, 30 Oct 2026 19:32:19 -0500 Subject: [PATCH] G706: avoid binary noise in fixed decimal text --- crates/functions/src/text.rs | 26 ++++++++++++++++++++------ docs/function-parity.md | 3 ++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/crates/functions/src/text.rs b/crates/functions/src/text.rs index df125f022..22fa94d7b 100644 --- a/crates/functions/src/text.rs +++ b/crates/functions/src/text.rs @@ -350,10 +350,10 @@ fn numbervalue(a: &[Arg], c: &mut dyn Ctx) -> R { /// Formats `x` (already rounded) with `decimals` places and optional thousands separators. fn format_fixed(x: f64, decimals: i64, commas: bool) -> String { let d = decimals.max(0) as usize; - let s = format!("{:.*}", d, x.abs()); + let s = x.abs().to_string(); let (int_part, frac) = match s.split_once('.') { - Some((i, f)) => (i.to_string(), Some(f.to_string())), - None => (s.clone(), None), + Some((i, f)) => (i, f), + None => (s.as_str(), ""), }; let int_part = if commas { let b = int_part.as_bytes(); @@ -366,12 +366,14 @@ fn format_fixed(x: f64, decimals: i64, commas: bool) -> String { } o } else { - int_part + int_part.to_string() }; let mut out = int_part; - if let Some(f) = frac { + if d > 0 { + let frac_len = frac.len().min(d); out.push('.'); - out.push_str(&f); + out.push_str(&frac[..frac_len]); + out.extend(std::iter::repeat_n('0', d - frac_len)); } out } @@ -1237,6 +1239,18 @@ mod tests { is_text(ev("DOLLAR", vec![n(0.5), n(0.0)]), "$1"); is_text(ev("USDOLLAR", vec![n(1.0)]), "$1.00"); is_text(ev("USDOLLAR", vec![n(-1234.567), n(1.0)]), "($1,234.6)"); + let fixed_16 = format!("1.1{}", "0".repeat(15)); + let dollar_16 = format!("$1.1{}", "0".repeat(15)); + let fixed_20 = format!("1.005{}", "0".repeat(17)); + let dollar_20 = format!("$1.005{}", "0".repeat(17)); + let fixed_127 = format!("1.1{}", "0".repeat(126)); + let dollar_127 = format!("$1.1{}", "0".repeat(126)); + is_text(ev("FIXED", vec![n(1.1), n(16.0)]), &fixed_16); + is_text(ev("DOLLAR", vec![n(1.1), n(16.0)]), &dollar_16); + is_text(ev("FIXED", vec![n(1.005), n(20.0)]), &fixed_20); + is_text(ev("DOLLAR", vec![n(1.005), n(20.0)]), &dollar_20); + is_text(ev("FIXED", vec![n(1.1), n(127.0)]), &fixed_127); + is_text(ev("DOLLAR", vec![n(1.1), n(127.0)]), &dollar_127); is_text(ev("T", vec![t("x")]), "x"); is_text(ev("T", vec![n(1.0)]), ""); } diff --git a/docs/function-parity.md b/docs/function-parity.md index bf57fcd3e..2bcb688bf 100644 --- a/docs/function-parity.md +++ b/docs/function-parity.md @@ -1,6 +1,6 @@ # Function parity: worksheet functions and formulas -> **Last reviewed:** 2026-10-15 · **Last updated:** 2026-10-25 · **Change:** minor (the LAMBDA row lists the recursion limit and its `#NUM!`, and the error a defined name holding an error gives when called; earlier: number text with accounting parentheses coerces like Excel 365; INDEX with one index on a 2-D reference is #REF! as in Excel; earlier: SUBTOTAL and AGGREGATE cap the cells they visit, #2175) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-31 · **Last updated:** 2026-10-31 · **Change:** minor (FIXED and DOLLAR preserve the rounded number's shortest decimal digits before padding high requested precision; measured in Windows Excel 16.0 build 20527, LCID 1033) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Excel's worksheet functions, category by category, against GridCraft's function library. Part of [target-app-parity.md](target-app-parity.md); gaps feed [gaps.md](gaps.md). @@ -123,6 +123,7 @@ workbooks; results checked in as data, not Excel files) and fixing what it finds | Date | Change | Summary | |---|---|---| +| 2026-10-31 | minor | FIXED and DOLLAR format the rounded number's shortest decimal representation before padding requested fractional zeroes, so 1.1 at 16 places and 1.005 at 20 places do not expose binary noise; measured in Windows Excel 16.0 build 20527, LCID 1033 | | 2026-10-25 | minor | LAMBDA row: a LAMBDA that calls itself more than 140 times, and a formula nested past the depth limit, give `#NUM!` (#1809; nesting gave `#CALC!` before). `=Foo(1)` where the defined name Foo holds `#N/A` gives `#N/A`, not `#NAME?` | | 2026-10-23 | minor | Number text coercion (VALUE, arithmetic, typed entry): a dollar sign before accounting parentheses (`$(1,234)`, `$ (5)`) and a percent sign after them (`(5)%`) read as negatives; `(-5)`, `((5))`, `($-5)`, `$5%`, `$(5)%` and dates or times in parentheses are `#VALUE!`, as observed in Excel 365 | | 2026-10-23 | minor | INDEX with only a row index on a reference of several rows and columns (`INDEX(A1:C3,2)`, `INDEX(A1:C3,0)`) is `#REF!`, as observed in Excel, so it also fails as a range endpoint and inside other functions; an empty column argument (`INDEX(A1:C3,2,)`) still gives the row. Checked that a column array with one index stays a 1×1 array (`TYPE` 64), which is what Excel returns |