Values: number text switches to scientific past 21 characters below 1, as Excel

Excel caches IMCSCH("13+5j") as 1.28233995888454E-06 and IMSECH("7+25j")'s imaginary part as
0.000241377759972715: below 1, a 15-digit decimal longer than 21 characters is written in
scientific notation. 9.99999999999999E-9&"" now gives 9.99999999999999E-09, not
0.00000000999999999999999. Affects every number-to-text conversion (&, CONCAT, TEXTJOIN, IM
functions, formula printing, labels).
This commit is contained in:
Local contributor committed 2026-10-14 12:59:37 -04:00
1 parent 4dd4ff9a93
commit 6ff543ce76
4 files changed
+64 -5

No files matched your search

+18
View File
@@ -274,6 +274,24 @@ fn iterative_calculation_of_text_and_errors() {
assert_eq!(t.get("B1"), Value::Error(CellError::Div0));
}
/// `&`, CONCAT and TEXTJOIN turn a number into at most 15 significant digits, decimal unless that
/// would need 16 integer digits or more than 21 characters below 1.
#[test]
fn text_joins_convert_numbers_to_15_significant_digits() {
let mut t = T::new();
for (formula, text) in [
("=(1E15-0.5)&\"\"", "1E+15"),
("=(999999999999999)&\"\"", "999999999999999"),
("=1234567890123456&\"\"", "1.23456789012346E+15"),
("=(9.99999999999999E-9)&\"\"", "9.99999999999999E-09"),
("=CONCAT(1/3*1E-5)", "3.33333333333333E-06"),
("=TEXTJOIN(\",\",TRUE,1/3*1E-4,0.1+0.2)", "0.0000333333333333333,0.3"),
] {
t.set("A1", formula);
assert_eq!(t.get("A1"), Value::from(text), "{formula}");
}
}
#[test]
fn circular_references_without_iterative_calculation() {
let mut t = T::new();
+39 -4
View File
@@ -349,8 +349,10 @@ pub fn sort_compare(a: &Value, b: &Value) -> Ordering {
}
}
/// Number → text the way Excel converts in `&` and `TEXT(x,"General")`-free contexts: up to 15
/// significant digits, scientific for very large or very small magnitudes.
/// Number → text the way Excel converts in `&`, CONCAT and TEXTJOIN: at most 15 significant
/// digits, rounded first; scientific from 1E+15, below 1E-9, and wherever the decimal would be
/// longer than 21 characters (`0.` plus 19 decimals: `0.0000692361330972509` stays decimal,
/// `1.28233995888454E-06` does not, as Excel caches them).
pub fn number_to_text(n: f64) -> String {
if n == 0.0 || !n.is_finite() {
return "0".into();
@@ -358,8 +360,12 @@ pub fn number_to_text(n: f64) -> String {
// Round to 15 significant digits first, then pick decimal or scientific from the rounded
// magnitude, so 999999999999999.5 reads `1E+15`, never a 16-digit integer.
let sci = format!("{:.14e}", n);
let mag: i32 = sci.split_once('e').and_then(|(_, e)| e.parse().ok()).unwrap_or(0);
if !(-9..15).contains(&mag) {
let (mant, exp) = sci.split_once('e').unwrap_or((&sci, "0"));
let mag: i32 = exp.parse().unwrap_or(0);
let sig = mant.bytes().filter(u8::is_ascii_digit).count() - mant.bytes().rev().take_while(|&b| b == b'0').count();
// Characters after `0.` for |n| < 1: the leading zeros, then the significant digits.
let decimals_below_one = -mag - 1 + sig as i32;
if !(-9..15).contains(&mag) || decimals_below_one > 19 {
return sci_text(n, 15);
}
// 15 significant digits, trailing zeros trimmed.
@@ -410,6 +416,35 @@ mod tests {
assert_eq!(number_to_text(1.0 / 3.0), "0.333333333333333");
}
/// Excel switches to scientific when the 15-digit decimal would be longer than 21 characters
/// (`0.` plus 19 decimals). Excel-verified: IMTAN, IMSECH, IMCSCH and IMEXP results cached by
/// Excel for Mac in the `formulas` project's test.xlsx (ENGINEERING!S53, R47, S37, V37, V39).
#[test]
fn number_text_small_magnitudes() {
assert_eq!(number_to_text(6.92361330972509e-5), "0.0000692361330972509");
assert_eq!(number_to_text(2.41377759972715e-4), "0.000241377759972715");
assert_eq!(number_to_text(1.28233995888454e-6), "1.28233995888454E-06");
assert_eq!(number_to_text(-4.48088972789593e-6), "-4.48088972789593E-06");
assert_eq!(number_to_text(5.98317105631764e-7), "5.98317105631764E-07");
assert_eq!(number_to_text(-2.99158552811404e-7), "-2.99158552811404E-07");
// Just below powers of ten: the 21-character limit, and the rounded magnitude.
assert_eq!(number_to_text(9.99999999999999e-5), "0.0000999999999999999");
assert_eq!(number_to_text(9.99999999999999e-6), "9.99999999999999E-06");
assert_eq!(number_to_text(9.9999999999999e-6), "0.0000099999999999999");
assert_eq!(number_to_text(9.99999999999999e-9), "9.99999999999999E-09");
assert_eq!(number_to_text(-9.99999999999999e-9), "-9.99999999999999E-09");
assert_eq!(number_to_text(9.99999999999999e-10), "9.99999999999999E-10");
assert_eq!(number_to_text(9.999999999999999e-6), "0.00001");
assert_eq!(number_to_text(0.00001), "0.00001");
assert_eq!(number_to_text(0.000001), "0.000001");
assert_eq!(number_to_text(1.5e-9), "0.0000000015");
// Large side: 15 significant digits, scientific from 1E+15.
assert_eq!(number_to_text(99999999999999.9), "99999999999999.9");
assert_eq!(number_to_text(999999999999999.4), "999999999999999");
assert_eq!(number_to_text(9999999999999998.0), "1E+16");
assert_eq!(number_to_text(9.99999999999999e15), "9.99999999999999E+15");
}
#[test]
fn coercion() {
assert_eq!(Value::from("12").to_number(), Ok(12.0));
+5
View File
@@ -1283,6 +1283,11 @@ mod tests {
cx(ev("IMCSC", vec![t("4+3i")]), -0.075_489_832_915_863_7, 0.064_877_471_370_635_5);
is_text(ev("IMSECH", vec![t("0")]), "1");
is_err(ev("IMCSCH", vec![t("0")]), CellError::Num);
// Excel-cached results (Excel for Mac, `formulas` project test.xlsx, ENGINEERING sheet): a
// part needing more than 21 characters as a decimal is written in scientific notation.
is_text(ev("IMCSCH", vec![t("13+5j")]), "1.28233995888454E-06+4.33496947416312E-06j");
is_text(ev("IMEXP", vec![t("-13+25j")]), "2.24044486393732E-06-2.99158552811404E-07j");
is_text(ev("IMSECH", vec![t("7+25j")]), "0.00180771853884451+0.000241377759972715j");
// sech z · cosh z = 1
let Value::Text(c) = ev("IMCOSH", vec![t("4+3i")]) else { panic!() };
let Value::Text(sc) = ev("IMSECH", vec![t("4+3i")]) else { panic!() };
+2 -1
View File
@@ -1,6 +1,6 @@
# Function parity: worksheet functions and formulas
> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (USDOLLAR added as a compatibility function; MIRR, COUNTIF and INDIRECT edge cases match Excel 365) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4
> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-14 · **Change:** minor (number-to-text in `&`, CONCAT, TEXTJOIN and the IM functions rounds to 15 digits first and switches to scientific like Excel) · **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).
@@ -108,6 +108,7 @@ workbooks; results checked in as data, not Excel files) and fixing what it finds
| Date | Change | Summary |
|---|---|---|
| 2026-10-14 | minor | Number-to-text (`&`, CONCAT, TEXTJOIN, VALUETOTEXT, the IM functions' complex text, and also formula printing, pivot and chart labels) rounds to 15 significant digits before choosing decimal or scientific, so `=(1E15-0.5)&""` is `1E+15`, not `1000000000000000`; and below 1 it switches to scientific when the decimal would be longer than 21 characters, as Excel caches IMCSCH/IMEXP/IMTAN results: `1.28233995888454E-06`, `9.99999999999999E-09` (was `0.00000000999999999999999`), but `0.0000692361330972509` |
| 2026-10-12 | minor | USDOLLAR added as a Compatibility function (not on Microsoft's list, so the counts don't change); MIRR with no positive flow is -1, COUNTIF's bare `"<="` / `">="` skip empty strings, INDIRECT with `a1` = FALSE gives `#REF!` for A1-style text (#738) |
| 2026-10-11 | minor | TEXT / number formats fixed against Excel-cached results: fraction literals and zero fractions, `0` denominators zero padded, `?` pads the thousands separator, booleans use the text section (226 mismatches → 0 in 352 TEXT cells) |
| 2026-10-11 | minor | Iterative calculation: circular references iterate up to Maximum Iterations / Maximum Change when enabled |