diff --git a/crates/functions/src/text.rs b/crates/functions/src/text.rs index 3adb4e4ef..7f1ba8731 100644 --- a/crates/functions/src/text.rs +++ b/crates/functions/src/text.rs @@ -427,8 +427,9 @@ fn fixed(a: &[Arg], _c: &mut dyn Ctx) -> R { fn dollar(a: &[Arg], _c: &mut dyn Ctx) -> R { let (r, d) = rounded(a, 2.0)?; + let negative = r < 0.0 || (r == 0.0 && d >= 0 && num(a, 0)? < 0.0); let body = format_fixed(r, d, true); - let out = if r < 0.0 { format!("(${body})") } else { format!("${body}") }; + let out = if negative { format!("(${body})") } else { format!("${body}") }; if out.len() > 254 { return Err(CellError::Value); } @@ -1326,6 +1327,21 @@ mod tests { is_err(ev("DOLLAR", vec![n(1e190), n(-1.0)]), CellError::Value); } + #[test] + fn dollar_keeps_negative_sign_when_nonnegative_precision_rounds_to_zero() { + is_text(ev("DOLLAR", vec![n(-0.0001), n(0.0)]), "($0)"); + is_text(ev("DOLLAR", vec![n(-0.0001), n(2.0)]), "($0.00)"); + is_text(ev("DOLLAR", vec![n(0.0), n(0.0)]), "$0"); + is_text(ev("DOLLAR", vec![n(-0.0), n(0.0)]), "$0"); + is_text(ev("DOLLAR", vec![n(0.0001), n(0.0)]), "$0"); + is_text(ev("DOLLAR", vec![n(-0.0001), n(-1.0)]), "$0"); + is_text(ev("DOLLAR", vec![n(-4.0), n(-1.0)]), "$0"); + is_text(ev("DOLLAR", vec![n(-5.0), n(-1.0)]), "($10)"); + is_text(ev("DOLLAR", vec![n(-0.4), n(0.0)]), "($0)"); + is_text(ev("DOLLAR", vec![n(-0.5), n(0.0)]), "($1)"); + is_text(ev("FIXED", vec![n(-0.0001), n(0.0)]), "0"); + } + #[test] fn before_after() { is_text(ev("TEXTBEFORE", vec![t("Red riding hood"), t(" ")]), "Red"); diff --git a/docs/function-parity.md b/docs/function-parity.md index 2a3549b75..cac3af4b3 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-31 · **Last updated:** 2026-10-31 · **Change:** minor (FIXED refuses results over 255 characters and DOLLAR refuses results over 254, matching the signed, decimal and separator boundary checks in Windows Excel 16.0 build 20527, LCID 1033; earlier: 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; earlier: ECMA.CEILING listed with USDOLLAR as not on Microsoft's list; IMSQRT reproduces the stray real part Excel gives for negative reals, and the other IM* differences from Excel are listed; the LAMBDA row lists the recursion limit and its `#NUM!`, and the error a defined name holding an error gives when called; number text with accounting parentheses coerces like Excel 365; INDEX with one index on a 2-D reference is #REF! as in Excel; 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 (DOLLAR keeps the negative layout when a negative input rounds to zero at nonnegative precision; negative precision that rounds to zero retains the positive layout, as measured in Windows Excel 16.0 build 20527, LCID 1033; earlier: FIXED refuses results over 255 characters and DOLLAR refuses results over 254, matching the signed, decimal and separator boundary checks in Windows Excel 16.0 build 20527, LCID 1033; earlier: ECMA.CEILING listed with USDOLLAR as not on Microsoft's list; IMSQRT reproduces the stray real part Excel gives for negative reals, and the other IM* differences from Excel are listed; the LAMBDA row lists the recursion limit and its `#NUM!`, and the error a defined name holding an error gives when called; number text with accounting parentheses coerces like Excel 365; INDEX with one index on a 2-D reference is #REF! as in Excel; SUBTOTAL and AGGREGATE cap the cells they visit, #2175) · **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). @@ -131,6 +131,7 @@ workbooks; results checked in as data, not Excel files) and fixing what it finds | 2026-10-31 | minor | UPPER, LOWER and PROPER use single-character casing: `UPPER("Straße")` keeps ß, `LOWER("İ")` returns i, and `PROPER("İSTANBUL")` returns Istanbul, matching Windows Excel 16.0 build 20527 in LCID 1033 | | 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-31 | minor | FIXED accepts output through 255 characters; DOLLAR accepts output through 254. Windows Excel 16.0 build 20527 (LCID 1033) confirmed the distinct cutoffs with grouped and ungrouped values, signs, currency layout and decimal places | +| 2026-10-31 | minor | DOLLAR uses the input sign for negative amounts that round to zero at nonnegative decimal places (`DOLLAR(-0.0001,0)` is `($0)`); when negative decimal places round to zero, Excel returns `$0`. Exact zero and negative zero stay unsigned; FIXED still returns `0` for `FIXED(-0.0001,0)` | | 2026-10-27 | minor | ECMA.CEILING added to the functions that are in our library but not on Microsoft's list (with USDOLLAR), so the counts don't change | | 2026-10-26 | minor | IMSQRT uses Excel's polar route with its cosine reduction, so `IMSQRT("-4")` is `1.22514845490862E-16+2i` as in Excel (exact `2i` before); the rows list the IM* differences still open | | 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?` |