From a6d70bcafb2a9d13516b5f2a9979758dd62ff2f9 Mon Sep 17 00:00:00 2001 From: neilcawse <8085959+neilcawse@users.noreply.github.com> Date: Mon, 12 Oct 2026 20:55:08 -0400 Subject: [PATCH 1/8] CSV/text export: every General number as & converts it (15 significant digits) Main writes plain General with Rust's shortest round-trip (0.30000000000000004, 100000000000000000000) and still narrows coloured/sectioned General and numbers in Text cells to the 11-character cell display (1.24412E+11). Export now formats through numfmt's format_value_full, which writes General as number_to_text does, replacing io.rs's plain-General special case. Docs: merge the duplicate CSV/text rows. --- crates/engine/src/display.rs | 23 +++++++++++--- crates/engine/src/io.rs | 60 ++++++++++++++++++++++++++++-------- crates/numfmt/src/lib.rs | 36 +++++++++++++++------- crates/numfmt/src/render.rs | 9 +++--- crates/numfmt/src/tests.rs | 2 +- docs/file-format-parity.md | 9 +++--- 6 files changed, 100 insertions(+), 39 deletions(-) diff --git a/crates/engine/src/display.rs b/crates/engine/src/display.rs index b90e42727..3e8d47c16 100644 --- a/crates/engine/src/display.rs +++ b/crates/engine/src/display.rs @@ -5,7 +5,7 @@ use std::sync::Mutex; use gridcraft_core::{CellRef, RangeRef, Value}; use gridcraft_model::{Sheet, Workbook}; -use gridcraft_numfmt::{Formatted, NumberFormat, format_value}; +use gridcraft_numfmt::{Formatted, NumberFormat, format_value, format_value_full}; /// Parsed number formats are cached by code. fn parsed(code: &str) -> NumberFormat { @@ -80,7 +80,8 @@ pub fn formula_hidden(wb: &Workbook, sheet: &Sheet, c: CellRef) -> bool { wb.styles.get(sheet.style_id(at)).protection.hidden } -/// The text a cell shows (full precision General; the grid narrows General to fit the column). +/// The text a cell shows (General up to the 11-character cell width; the grid narrows General +/// further to fit the column). pub fn cell_text(wb: &Workbook, sheet: &Sheet, c: CellRef) -> String { if sheet.show_formulas && let Some(f) = sheet.cell(c).and_then(|x| x.formula.as_ref()) @@ -88,14 +89,26 @@ pub fn cell_text(wb: &Workbook, sheet: &Sheet, c: CellRef) -> String { { return format!("={}", f.text); } - value_text(wb, sheet, c) + shown_text(wb, sheet, c) } /// A cell's value with its number format applied, independent of column width: no `#####` -/// and no General narrowing (Save As CSV/Text writes this). Formulas give their result. +/// and no General narrowing (Save As CSV/Text writes this). Formulas give their result. A number +/// shown as General (also through a coloured or sectioned General, or a Text format) reads as `&` +/// turns it into text (`124412140710`, not `1.24412E+11`); Excel writes the cell display there. pub fn value_text(wb: &Workbook, sheet: &Sheet, c: CellRef) -> String { let style = wb.styles.get(sheet.style_id(c)); - let f = format(&sheet.value(c), style.num_fmt.as_str(), wb); + with_fill_space(format_value_full(&sheet.value(c), &parsed(style.num_fmt.as_str()), wb.date_system)) +} + +/// A cell's value through its number format, General at the 11-character cell width. +fn shown_text(wb: &Workbook, sheet: &Sheet, c: CellRef) -> String { + let style = wb.styles.get(sheet.style_id(c)); + with_fill_space(format(&sheet.value(c), style.num_fmt.as_str(), wb)) +} + +/// The formatted text with a single space where a `*x` repeat fill would expand. +fn with_fill_space(f: Formatted) -> String { match f.fill { Some((_, pos)) => { let mut t = f.text; diff --git a/crates/engine/src/io.rs b/crates/engine/src/io.rs index 3e84036cb..0cf40735f 100644 --- a/crates/engine/src/io.rs +++ b/crates/engine/src/io.rs @@ -231,20 +231,10 @@ pub fn source_dir(path: &str) -> Option { /// Save As CSV/Text writes "the text and values as they are displayed in cells" with "all /// characters in each cell" (Microsoft Support, *Excel formatting and features that are not /// transferred to other file formats*): number formats applied, never column-width `#####`. +/// One intentional difference: a number shown as General is written as `&` converts it (15 +/// significant digits), not narrowed to the cell width, so long identifiers survive. fn csv(wb: &Workbook, sh: &Sheet, delimiter: u8) -> Result> { - gridcraft_xlsx::write_csv(sh, delimiter, |c| { - // General's grid-width formatting rounds large identifiers to scientific notation. - // Delimited files have no column width, so preserve the whole numeric value instead. - if wb.styles.get(sh.style_id(c)).num_fmt.as_str().eq_ignore_ascii_case("General") - && let gridcraft_core::Value::Number(number) = sh.value(c) - && number.is_finite() - { - number.to_string() - } else { - crate::display::value_text(wb, sh, c) - } - }) - .map_err(|e| EngineError::Other(e.to_string())) + gridcraft_xlsx::write_csv(sh, delimiter, |c| crate::display::value_text(wb, sh, c)).map_err(|e| EngineError::Other(e.to_string())) } fn esc(s: &str) -> String { @@ -711,6 +701,50 @@ mod tests { } } + /// A number shown as General is written as `&` converts it (15 significant digits), whatever + /// colour, sections or literals the format adds, and in a Text cell. Excel writes the cell + /// display (`1.24412E+11`, `0.333333333`); GridCraft keeps the digits so part codes survive. + #[test] + fn delimited_exports_write_general_numbers_as_text_converts_them() { + use gridcraft_core::Value; + use gridcraft_model::{Cell, NumFmt, StyleId}; + let mut wb = Workbook::new(); + let mut styled = |code: &str| wb.styles.derive(StyleId::DEFAULT, |s| s.num_fmt = NumFmt::new(code)); + let (blue, signed, red, text, unit) = + (styled("[Blue]General"), styled("General;-General"), styled("General;[Red]-General"), styled("@"), styled("General\" mm\"")); + let code = 124412140710.0; + let sh = wb.sheet_mut(0).unwrap(); + let cells = [ + (Value::Number(1.0 / 3.0), StyleId::DEFAULT), + (Value::Number(0.1 + 0.2), StyleId::DEFAULT), + (Value::Number(1e20), StyleId::DEFAULT), + (Value::Number(code), blue), + (Value::Number(-1.0 / 3.0), blue), + (Value::Number(-code), signed), + (Value::Number(-code), red), + (Value::Number(code), text), + (Value::Number(code), unit), + ]; + for (col, (value, style)) in cells.into_iter().enumerate() { + sh.set_cell(CellRef::new(0, col as u32), Cell { value, formula: None, style }); + } + for (path, sep) in [("out.csv", ","), ("out.txt", "\t")] { + let output = String::from_utf8(save_bytes(&wb, path).unwrap()).unwrap(); + let expected = [ + "0.333333333333333", + "0.3", + "1E+20", + "124412140710", + "-0.333333333333333", + "-124412140710", + "-124412140710", + "124412140710", + "124412140710 mm", + ]; + assert_eq!(output, format!("{}\r\n", expected.join(sep))); + } + } + #[test] fn template_extension_saves_template_content_type() { let bytes = save_bytes(&Workbook::new(), "Budget.xltx").unwrap(); diff --git a/crates/numfmt/src/lib.rs b/crates/numfmt/src/lib.rs index 6280e1d26..22f288066 100644 --- a/crates/numfmt/src/lib.rs +++ b/crates/numfmt/src/lib.rs @@ -256,23 +256,25 @@ impl NumberFormat { } /// Formats a number. The flag is true when the value cannot be shown (date out of range). - fn format_number(&self, n: f64, sys: DateSystem) -> (Formatted, bool) { - let general = |n: f64| Formatted { - text: format_general_fit(n, GENERAL_WIDTH).unwrap_or_else(|| OVERFLOW.into()), - color: None, - fill: None, - numeric: true, + /// With `full`, General is not narrowed to the cell width: it reads as `&` turns the number + /// into text (15 significant digits). + fn format_number(&self, n: f64, sys: DateSystem, full: bool) -> (Formatted, bool) { + let general_text = |n: f64, width: usize| { + if full { Some(gridcraft_core::number_to_text(n)) } else { format_general_fit(n, width) } }; + let general = + |n: f64| Formatted { text: general_text(n, GENERAL_WIDTH).unwrap_or_else(|| OVERFLOW.into()), color: None, fill: None, numeric: true }; if self.is_general() { return (general(n), false); } let Some((sec, drop_sign)) = self.select(n) else { return (general(n), false) }; let minus = n < 0.0 && !drop_sign; let out: Option = match sec.kind { - SecKind::Text => Some(render::render_text(sec, &format_general_fit(n, GENERAL_WIDTH).unwrap_or_default())), + SecKind::Text => Some(render::render_text(sec, &general_text(n, GENERAL_WIDTH).unwrap_or_default())), SecKind::General => { let width = if minus { GENERAL_WIDTH - 1 } else { GENERAL_WIDTH }; - Some(render::render_general(sec, n.abs(), width)) + let text = general_text(n.abs(), width).unwrap_or_else(|| "#".repeat(width.clamp(1, 11))); + Some(render::render_general(sec, &text)) } SecKind::Date => { if minus { @@ -306,16 +308,28 @@ impl NumberFormat { /// Formats a cell value for display. pub fn format_value(v: &Value, fmt: &NumberFormat, sys: DateSystem) -> Formatted { + format_value_with(v, fmt, sys, false) +} + +/// Formats a cell value as Save As CSV/Text writes it: like [`format_value`], except that a number +/// shown as General (a General or Text section, with or without colour and literals) is not +/// narrowed to the cell width but written as `&` turns it into text: `124412140710`, not +/// `1.24412E+11`. Excel writes the cell display here; GridCraft keeps the digits. +pub fn format_value_full(v: &Value, fmt: &NumberFormat, sys: DateSystem) -> Formatted { + format_value_with(v, fmt, sys, true) +} + +fn format_value_with(v: &Value, fmt: &NumberFormat, sys: DateSystem, full: bool) -> Formatted { match v { Value::Empty => Formatted::default(), Value::Number(n) if !n.is_finite() => Formatted { text: CellError::Num.as_str().into(), ..Formatted::default() }, - Value::Number(n) => fmt.format_number(*n, sys).0, + Value::Number(n) => fmt.format_number(*n, sys, full).0, Value::Text(t) => fmt.format_text(t), Value::Bool(b) => Formatted { text: if *b { "TRUE" } else { "FALSE" }.into(), ..Formatted::default() }, Value::Error(e) => Formatted { text: e.as_str().into(), ..Formatted::default() }, Value::Array(a) => match a.data.first() { Some(Value::Array(_)) | None => Formatted::default(), - Some(first) => format_value(first, fmt, sys), + Some(first) => format_value_with(first, fmt, sys, full), }, } } @@ -417,7 +431,7 @@ pub fn text_function(v: &Value, code: &str, sys: DateSystem) -> Result Out { out } -/// Renders a number through a General section (literals around the General keyword). -pub(crate) fn render_general(sec: &Section, v: f64, width: usize) -> Out { +/// Renders a number through a General section: `general` (the number already written as General) +/// with the section's literals around it. +pub(crate) fn render_general(sec: &Section, general: &str) -> Out { let mut out = Out::default(); for t in &sec.toks { match t { - Tok::General => out.push(&format_general_fit(v, width).unwrap_or_else(|| "#".repeat(width.clamp(1, 11)))), + Tok::General => out.push(general), Tok::Fill(c) => out.fill(*c), other => out.push(&other.literal_text()), } diff --git a/crates/numfmt/src/tests.rs b/crates/numfmt/src/tests.rs index e5687f5bd..f45868664 100644 --- a/crates/numfmt/src/tests.rs +++ b/crates/numfmt/src/tests.rs @@ -637,7 +637,7 @@ fn text_uncached(v: &Value, code: &str, sys: DateSystem) -> Result **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** trivial (XLSX chart sheets are kept on save (#727); previously minor: CSV save keeps the separator it was opened with) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-13 · **Change:** minor (CSV/text export writes every General number as `&` converts it) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Every format Excel opens or saves, with GridCraft's read and write support, fidelity and tests. Part of [target-app-parity.md](target-app-parity.md). **File formats: ~50% ready, 80–120 h** @@ -41,10 +41,8 @@ and repair-free opening remain unverified. | Format | Excel | GridCraft read | GridCraft write | Notes | |---|---|---|---|---| -| `.csv` UTF-8 / Windows / Mac / MS-DOS | R/W | yes (UTF-8 and UTF-16 BOMs, UTF-8, Windows-1252 fallback; delimiter detection) | yes (UTF-8 without BOM, CRLF; a file opened as CSV is saved with the separator it was read with, so a `;` file stays `;`; otherwise `,`) | Excel writes the **displayed** values; we write raw values. No encoding choice on save. Re-import of a single-column export splits text (#127) A used range that would write more than 512 MiB (two far-apart cells span the whole rectangle) is refused with an error instead of being expanded (#528). | -| `.txt` tab-delimited, Unicode text | R/W | yes (`.txt`, `.tsv`, `.tab`) | yes | Text Import Wizard options partly via Text to Columns | -| `.csv` UTF-8 / Windows / Mac / MS-DOS | R/W | yes (UTF-8 and UTF-16 BOMs, UTF-8, Windows-1252 fallback; delimiter detection) | yes (UTF-8 without BOM, CRLF) | Writes the **displayed** values like Excel (number formats applied, formula results, never column-width `#####`). No encoding choice on save. Re-import of a single-column export splits text (#127) A used range that would write more than 512 MiB (two far-apart cells span the whole rectangle) is refused with an error instead of being expanded (#528). | -| `.txt` tab-delimited, Unicode text | R/W | yes (`.txt`, `.tsv`, `.tab`) | yes (displayed values) | Text Import Wizard options partly via Text to Columns | +| `.csv` UTF-8 / Windows / Mac / MS-DOS | R/W | yes (UTF-8 and UTF-16 BOMs, UTF-8, Windows-1252 fallback; delimiter detection) | yes (UTF-8 without BOM, CRLF; a file opened as CSV is saved with the separator it was read with, so a `;` file stays `;`; otherwise `,`) | Formatted numbers and formula results are written as displayed, as in Excel (never column-width `#####`). **Intentional difference:** Excel writes a General number as the cell displays it (`1.24412E+11`, `0.333333333`); GridCraft writes it to 15 significant digits, as `&` converts it (`124412140710`, `0.333333333333333`), so part codes survive a round trip. The same applies to a coloured or sectioned General (`[Blue]General`, `General;[Red]-General`) and to a number in a Text (`@`) cell. No encoding choice on save. Re-import of a single-column export splits text (#127). A used range that would write more than 512 MiB (two far-apart cells span the whole rectangle) is refused with an error instead of being expanded (#528). | +| `.txt` tab-delimited, Unicode text | R/W | yes (`.txt`, `.tsv`, `.tab`) | yes (displayed values, with General numbers as for `.csv`) | Text Import Wizard options partly via Text to Columns | | `.prn` formatted text (space-delimited) | R/W | no | no | | | `.dif`, `.slk` (SYLK) | R/W | no | no | Small, legacy | | `.htm` / `.html` web page | R/W (Windows) | yes, via Get Data ▸ From HTML | yes (one sheet as a table; a used range taller than 100,001 rows fails with an error instead of being cut short) | Formatting not exported | @@ -85,6 +83,7 @@ refresh. Counted under features (Get & Transform, ~10% ready), not here. | Date | Change | Summary | |---|---|---| +| 2026-10-13 | minor | CSV/text export writes General numbers as `&` converts them (15 significant digits: `0.3`, not `0.30000000000000004`) also through coloured or sectioned General and Text formats (`[Blue]General` still wrote `1.24412E+11`). Duplicate CSV/text rows merged. Intentional difference from Excel, which writes the cell display | | 2026-10-12 | trivial | XLSX chart sheets are kept on save (#727) | | 2026-10-12 | trivial | CSV and TSV export is capped at 512 MiB and refuses a larger used range with an error (#528) | | 2026-10-12 | minor | `.xls` import reads the calculation settings (CalcMode, CalcIter, CalcCount, CalcDelta, CalcPrecision), so circular models calculate instead of showing `#CIRC!` (#724) | From 9331dbb47472b3da94c68577f9211d1fd3ab8947 Mon Sep 17 00:00:00 2001 From: Local contributor Date: Wed, 14 Oct 2026 08:23:36 -0400 Subject: [PATCH 2/8] Values: number text rounds to 15 digits before choosing decimal or scientific number_to_text picked the decimal branch for values in [999999999999999.5, 1e15) and then rounded them up to the 16-digit integer 1000000000000000. It now rounds to 15 significant digits first and chooses the notation from the rounded exponent, so those values read 1E+15 (and -1E+15). --- crates/core/src/value.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/core/src/value.rs b/crates/core/src/value.rs index cfef7ac57..42f7912fb 100644 --- a/crates/core/src/value.rs +++ b/crates/core/src/value.rs @@ -355,12 +355,14 @@ pub fn number_to_text(n: f64) -> String { if n == 0.0 || !n.is_finite() { return "0".into(); } - let a = n.abs(); - if !(1e-9..1e15).contains(&a) && a.round() != a || a >= 1e15 { + // 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) { return sci_text(n, 15); } // 15 significant digits, trailing zeros trimmed. - let mag = a.log10().floor() as i32; let decimals = (14 - mag).clamp(0, 30) as usize; let s = format!("{:.*}", decimals, n); trim_decimal(&s) @@ -399,6 +401,12 @@ mod tests { assert_eq!(number_to_text(1234567890123456.0), "1.23456789012346E+15"); assert_eq!(number_to_text(0.0001), "0.0001"); assert_eq!(number_to_text(1e-10), "1E-10"); + assert_eq!(number_to_text(1e15 - 0.5), "1E+15"); + assert_eq!(number_to_text(1e15 - 0.1), "1E+15"); + assert_eq!(number_to_text(-(1e15 - 0.5)), "-1E+15"); + assert_eq!(number_to_text(999999999999999.0), "999999999999999"); + assert_eq!(number_to_text(9.999999999999999e-10), "0.000000001"); + assert_eq!(number_to_text(-9.999999999999999e-10), "-0.000000001"); assert_eq!(number_to_text(1.0 / 3.0), "0.333333333333333"); } From 4dd4ff9a9358e071e3109790c3fe0c69785c19a0 Mon Sep 17 00:00:00 2001 From: Local contributor Date: Wed, 14 Oct 2026 08:23:36 -0400 Subject: [PATCH 3/8] CSV/text export: integers below 2^53 keep every digit Writing every General number as & converts it rounded 16-digit integers such as card and order numbers to 1.23456789012346E+15, so opening a CSV and saving it again changed the data. Integers that f64 holds exactly (|x| < 2^53) are now written with all their digits, as before; larger integers and non-integers keep the 15-significant-digit & text. --- crates/engine/src/io.rs | 16 ++++++++++++++-- crates/numfmt/src/lib.rs | 14 +++++++++++--- docs/file-format-parity.md | 4 ++-- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/io.rs b/crates/engine/src/io.rs index 0cf40735f..2b711f573 100644 --- a/crates/engine/src/io.rs +++ b/crates/engine/src/io.rs @@ -695,15 +695,27 @@ mod tests { sheet.set_cell(CellRef::new(0, 0), Cell { value: Value::Number(124412140710.0), ..Default::default() }); sheet.set_cell(CellRef::new(0, 1), Cell { value: Value::Number(1.23456789012345), ..Default::default() }); sheet.set_cell(CellRef::new(0, 2), Cell { value: Value::Number(124412140710.0), style: scientific, ..Default::default() }); + // Integers f64 holds exactly (below 2^53) keep every digit; larger integers and + // non-integers get the 15 significant digits `&` gives. + for (col, n) in [(3, 1234567890123456.0), (4, -9007199254740991.0), (5, 12345678901234567.0), (6, 1e15 - 0.5)] { + sheet.set_cell(CellRef::new(0, col), Cell { value: Value::Number(n), ..Default::default() }); + } for (path, separator) in [("data.csv", ","), ("data.tsv", "\t")] { let output = String::from_utf8(save_bytes(&workbook, path).unwrap()).unwrap(); - assert_eq!(output, format!("124412140710{separator}1.23456789012345{separator}1.24E+11\r\n")); + assert_eq!( + output, + format!( + "124412140710{s}1.23456789012345{s}1.24E+11{s}1234567890123456{s}-9007199254740991{s}1.23456789012346E+16{s}1E+15\r\n", + s = separator + ) + ); } } /// A number shown as General is written as `&` converts it (15 significant digits), whatever /// colour, sections or literals the format adds, and in a Text cell. Excel writes the cell - /// display (`1.24412E+11`, `0.333333333`); GridCraft keeps the digits so part codes survive. + /// display (`1.24412E+11`, `0.333333333`); GridCraft keeps the digits so part codes survive + /// (integers below 2^53 keep all their digits; see the test above). #[test] fn delimited_exports_write_general_numbers_as_text_converts_them() { use gridcraft_core::Value; diff --git a/crates/numfmt/src/lib.rs b/crates/numfmt/src/lib.rs index 22f288066..b43b8d888 100644 --- a/crates/numfmt/src/lib.rs +++ b/crates/numfmt/src/lib.rs @@ -256,11 +256,19 @@ impl NumberFormat { } /// Formats a number. The flag is true when the value cannot be shown (date out of range). - /// With `full`, General is not narrowed to the cell width: it reads as `&` turns the number - /// into text (15 significant digits). + /// With `full`, General is not narrowed to the cell width: an integer f64 holds exactly + /// (|n| < 2^53) is written with all its digits, so 16-digit card and order numbers survive a + /// CSV round trip; any other number reads as `&` turns it into text (15 significant digits). fn format_number(&self, n: f64, sys: DateSystem, full: bool) -> (Formatted, bool) { + const EXACT_INT: f64 = 9_007_199_254_740_992.0; // 2^53 let general_text = |n: f64, width: usize| { - if full { Some(gridcraft_core::number_to_text(n)) } else { format_general_fit(n, width) } + if !full { + format_general_fit(n, width) + } else if n != 0.0 && n.fract() == 0.0 && n.abs() < EXACT_INT { + Some(format!("{}", n as i64)) + } else { + Some(gridcraft_core::number_to_text(n)) + } }; let general = |n: f64| Formatted { text: general_text(n, GENERAL_WIDTH).unwrap_or_else(|| OVERFLOW.into()), color: None, fill: None, numeric: true }; diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index 7cefaac18..516130915 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -41,7 +41,7 @@ and repair-free opening remain unverified. | Format | Excel | GridCraft read | GridCraft write | Notes | |---|---|---|---|---| -| `.csv` UTF-8 / Windows / Mac / MS-DOS | R/W | yes (UTF-8 and UTF-16 BOMs, UTF-8, Windows-1252 fallback; delimiter detection) | yes (UTF-8 without BOM, CRLF; a file opened as CSV is saved with the separator it was read with, so a `;` file stays `;`; otherwise `,`) | Formatted numbers and formula results are written as displayed, as in Excel (never column-width `#####`). **Intentional difference:** Excel writes a General number as the cell displays it (`1.24412E+11`, `0.333333333`); GridCraft writes it to 15 significant digits, as `&` converts it (`124412140710`, `0.333333333333333`), so part codes survive a round trip. The same applies to a coloured or sectioned General (`[Blue]General`, `General;[Red]-General`) and to a number in a Text (`@`) cell. No encoding choice on save. Re-import of a single-column export splits text (#127). A used range that would write more than 512 MiB (two far-apart cells span the whole rectangle) is refused with an error instead of being expanded (#528). | +| `.csv` UTF-8 / Windows / Mac / MS-DOS | R/W | yes (UTF-8 and UTF-16 BOMs, UTF-8, Windows-1252 fallback; delimiter detection) | yes (UTF-8 without BOM, CRLF; a file opened as CSV is saved with the separator it was read with, so a `;` file stays `;`; otherwise `,`) | Formatted numbers and formula results are written as displayed, as in Excel (never column-width `#####`). **Intentional difference:** Excel writes a General number as the cell displays it (`1.24412E+11`, `0.333333333`); GridCraft writes an integer below 2^53 (which f64 holds exactly) with all its digits (`124412140710`, `1234567890123456`), so part, card and order numbers survive a round trip, and any other number to 15 significant digits, as `&` converts it (`0.333333333333333`, `1.23456789012346E+16`). The same applies to a coloured or sectioned General (`[Blue]General`, `General;[Red]-General`) and to a number in a Text (`@`) cell. No encoding choice on save. Re-import of a single-column export splits text (#127). A used range that would write more than 512 MiB (two far-apart cells span the whole rectangle) is refused with an error instead of being expanded (#528). | | `.txt` tab-delimited, Unicode text | R/W | yes (`.txt`, `.tsv`, `.tab`) | yes (displayed values, with General numbers as for `.csv`) | Text Import Wizard options partly via Text to Columns | | `.prn` formatted text (space-delimited) | R/W | no | no | | | `.dif`, `.slk` (SYLK) | R/W | no | no | Small, legacy | @@ -83,7 +83,7 @@ refresh. Counted under features (Get & Transform, ~10% ready), not here. | Date | Change | Summary | |---|---|---| -| 2026-10-13 | minor | CSV/text export writes General numbers as `&` converts them (15 significant digits: `0.3`, not `0.30000000000000004`) also through coloured or sectioned General and Text formats (`[Blue]General` still wrote `1.24412E+11`). Duplicate CSV/text rows merged. Intentional difference from Excel, which writes the cell display | +| 2026-10-13 | minor | CSV/text export writes General numbers as `&` converts them (15 significant digits: `0.3`, not `0.30000000000000004`; integers below 2^53 keep every digit) also through coloured or sectioned General and Text formats (`[Blue]General` still wrote `1.24412E+11`). Duplicate CSV/text rows merged. Intentional difference from Excel, which writes the cell display | | 2026-10-12 | trivial | XLSX chart sheets are kept on save (#727) | | 2026-10-12 | trivial | CSV and TSV export is capped at 512 MiB and refuses a larger used range with an error (#528) | | 2026-10-12 | minor | `.xls` import reads the calculation settings (CalcMode, CalcIter, CalcCount, CalcDelta, CalcPrecision), so circular models calculate instead of showing `#CIRC!` (#724) | From 6ff543ce764a977e4e4ee6a229df4b6bf9b706b2 Mon Sep 17 00:00:00 2001 From: Local contributor Date: Wed, 14 Oct 2026 12:59:37 -0400 Subject: [PATCH 4/8] 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). --- crates/calc/src/tests.rs | 18 ++++++++++++ crates/core/src/value.rs | 43 ++++++++++++++++++++++++++--- crates/functions/src/engineering.rs | 5 ++++ docs/function-parity.md | 3 +- 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 196c57de7..4f64e626e 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -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(); diff --git a/crates/core/src/value.rs b/crates/core/src/value.rs index 42f7912fb..a84bdda61 100644 --- a/crates/core/src/value.rs +++ b/crates/core/src/value.rs @@ -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)); diff --git a/crates/functions/src/engineering.rs b/crates/functions/src/engineering.rs index f34ce99f0..42e9c2f04 100644 --- a/crates/functions/src/engineering.rs +++ b/crates/functions/src/engineering.rs @@ -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!() }; diff --git a/docs/function-parity.md b/docs/function-parity.md index 760ffd6ac..b0bd968ed 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-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 | From bf9b3cb9b14be0498d8fc0b563167b6909ca5b88 Mon Sep 17 00:00:00 2001 From: Local contributor Date: Wed, 14 Oct 2026 12:59:37 -0400 Subject: [PATCH 5/8] CSV/text export: integers below 1e16 keep every digit; no exponents The 2^53 cutoff turned exact 16-digit integers (9007199254740992, 9007199254740994) into 9.00719925474099E+15. A General number is now written with &'s 15 digits but never with an exponent: reopening a CSV gives a scientific number a 0.00E+00 format that rounds it on the next save. 1E+15 and up are written as the shortest round-trip digits (every integer below 1e16 exactly, main's output above), small numbers as their 15 digits in full. --- crates/engine/src/display.rs | 5 ++-- crates/engine/src/io.rs | 47 ++++++++++++++++++++++++------------ crates/numfmt/src/lib.rs | 36 +++++++++++++++------------ docs/file-format-parity.md | 6 ++--- 4 files changed, 59 insertions(+), 35 deletions(-) diff --git a/crates/engine/src/display.rs b/crates/engine/src/display.rs index 3e8d47c16..3b891caef 100644 --- a/crates/engine/src/display.rs +++ b/crates/engine/src/display.rs @@ -94,8 +94,9 @@ pub fn cell_text(wb: &Workbook, sheet: &Sheet, c: CellRef) -> String { /// A cell's value with its number format applied, independent of column width: no `#####` /// and no General narrowing (Save As CSV/Text writes this). Formulas give their result. A number -/// shown as General (also through a coloured or sectioned General, or a Text format) reads as `&` -/// turns it into text (`124412140710`, not `1.24412E+11`); Excel writes the cell display there. +/// shown as General (also through a coloured or sectioned General, or a Text format) is written +/// as [`format_value_full`] writes it: `&`'s 15 significant digits without an exponent, an integer +/// below 1e16 with every digit (`124412140710`, not `1.24412E+11`). Excel writes the cell display. pub fn value_text(wb: &Workbook, sheet: &Sheet, c: CellRef) -> String { let style = wb.styles.get(sheet.style_id(c)); with_fill_space(format_value_full(&sheet.value(c), &parsed(style.num_fmt.as_str()), wb.date_system)) diff --git a/crates/engine/src/io.rs b/crates/engine/src/io.rs index 2b711f573..b52c8cb90 100644 --- a/crates/engine/src/io.rs +++ b/crates/engine/src/io.rs @@ -231,8 +231,10 @@ pub fn source_dir(path: &str) -> Option { /// Save As CSV/Text writes "the text and values as they are displayed in cells" with "all /// characters in each cell" (Microsoft Support, *Excel formatting and features that are not /// transferred to other file formats*): number formats applied, never column-width `#####`. -/// One intentional difference: a number shown as General is written as `&` converts it (15 -/// significant digits), not narrowed to the cell width, so long identifiers survive. +/// One intentional difference: a number shown as General is not narrowed to the cell width but +/// written as `&` converts it (15 significant digits) without an exponent, and an integer below +/// 1e16 with every digit, so long identifiers survive a reopen and save +/// (`gridcraft_numfmt::format_value_full`). fn csv(wb: &Workbook, sh: &Sheet, delimiter: u8) -> Result> { gridcraft_xlsx::write_csv(sh, delimiter, |c| crate::display::value_text(wb, sh, c)).map_err(|e| EngineError::Other(e.to_string())) } @@ -695,27 +697,40 @@ mod tests { sheet.set_cell(CellRef::new(0, 0), Cell { value: Value::Number(124412140710.0), ..Default::default() }); sheet.set_cell(CellRef::new(0, 1), Cell { value: Value::Number(1.23456789012345), ..Default::default() }); sheet.set_cell(CellRef::new(0, 2), Cell { value: Value::Number(124412140710.0), style: scientific, ..Default::default() }); - // Integers f64 holds exactly (below 2^53) keep every digit; larger integers and - // non-integers get the 15 significant digits `&` gives. - for (col, n) in [(3, 1234567890123456.0), (4, -9007199254740991.0), (5, 12345678901234567.0), (6, 1e15 - 0.5)] { - sheet.set_cell(CellRef::new(0, col), Cell { value: Value::Number(n), ..Default::default() }); + // Every integer below 1e16 that f64 holds (also 2^53 and the even integers above it) keeps + // every digit; larger numbers keep the shortest digits that read back as the same value, + // and a number `&` would write with an exponent is written out in full, so reopening the + // file can't give it a scientific format that then rounds it on the next save. + let cells = [ + (1234567890123456.0, "1234567890123456"), + (-9007199254740991.0, "-9007199254740991"), + (9007199254740992.0, "9007199254740992"), + (9007199254740994.0, "9007199254740994"), + (9123456789012346.0, "9123456789012346"), + (-9999999999999998.0, "-9999999999999998"), + (12345678901234567.0, "12345678901234568"), + (1e20, "100000000000000000000"), + (1e15 - 0.5, "999999999999999.5"), + (1.0 / 3.0 * 1e-6, "0.000000333333333333333"), + (1.23456789012346e-10, "0.000000000123456789012346"), + ]; + for (col, (n, _)) in cells.iter().enumerate() { + sheet.set_cell(CellRef::new(0, col as u32 + 3), Cell { value: Value::Number(*n), ..Default::default() }); } for (path, separator) in [("data.csv", ","), ("data.tsv", "\t")] { let output = String::from_utf8(save_bytes(&workbook, path).unwrap()).unwrap(); - assert_eq!( - output, - format!( - "124412140710{s}1.23456789012345{s}1.24E+11{s}1234567890123456{s}-9007199254740991{s}1.23456789012346E+16{s}1E+15\r\n", - s = separator - ) - ); + let tail: Vec<&str> = cells.iter().map(|(_, text)| *text).collect(); + assert_eq!(output, format!("124412140710{s}1.23456789012345{s}1.24E+11{s}{}\r\n", tail.join(separator), s = separator)); + // Reopening the file and saving it again changes nothing. + let (reopened, _) = open_bytes(path, output.as_bytes()).unwrap(); + assert_eq!(String::from_utf8(save_bytes(&reopened, path).unwrap()).unwrap(), output); } } /// A number shown as General is written as `&` converts it (15 significant digits), whatever /// colour, sections or literals the format adds, and in a Text cell. Excel writes the cell /// display (`1.24412E+11`, `0.333333333`); GridCraft keeps the digits so part codes survive - /// (integers below 2^53 keep all their digits; see the test above). + /// (integers below 1e16 keep all their digits, and nothing gets an exponent; see the test above). #[test] fn delimited_exports_write_general_numbers_as_text_converts_them() { use gridcraft_core::Value; @@ -731,6 +746,7 @@ mod tests { (Value::Number(0.1 + 0.2), StyleId::DEFAULT), (Value::Number(1e20), StyleId::DEFAULT), (Value::Number(code), blue), + (Value::Number(9007199254740994.0), blue), (Value::Number(-1.0 / 3.0), blue), (Value::Number(-code), signed), (Value::Number(-code), red), @@ -745,8 +761,9 @@ mod tests { let expected = [ "0.333333333333333", "0.3", - "1E+20", + "100000000000000000000", "124412140710", + "9007199254740994", "-0.333333333333333", "-124412140710", "-124412140710", diff --git a/crates/numfmt/src/lib.rs b/crates/numfmt/src/lib.rs index b43b8d888..583b5aa14 100644 --- a/crates/numfmt/src/lib.rs +++ b/crates/numfmt/src/lib.rs @@ -256,20 +256,9 @@ impl NumberFormat { } /// Formats a number. The flag is true when the value cannot be shown (date out of range). - /// With `full`, General is not narrowed to the cell width: an integer f64 holds exactly - /// (|n| < 2^53) is written with all its digits, so 16-digit card and order numbers survive a - /// CSV round trip; any other number reads as `&` turns it into text (15 significant digits). + /// With `full`, General is not narrowed to the cell width but written by [`general_full`]. fn format_number(&self, n: f64, sys: DateSystem, full: bool) -> (Formatted, bool) { - const EXACT_INT: f64 = 9_007_199_254_740_992.0; // 2^53 - let general_text = |n: f64, width: usize| { - if !full { - format_general_fit(n, width) - } else if n != 0.0 && n.fract() == 0.0 && n.abs() < EXACT_INT { - Some(format!("{}", n as i64)) - } else { - Some(gridcraft_core::number_to_text(n)) - } - }; + let general_text = |n: f64, width: usize| if full { Some(general_full(n)) } else { format_general_fit(n, width) }; let general = |n: f64| Formatted { text: general_text(n, GENERAL_WIDTH).unwrap_or_else(|| OVERFLOW.into()), color: None, fill: None, numeric: true }; if self.is_general() { @@ -321,12 +310,29 @@ pub fn format_value(v: &Value, fmt: &NumberFormat, sys: DateSystem) -> Formatted /// Formats a cell value as Save As CSV/Text writes it: like [`format_value`], except that a number /// shown as General (a General or Text section, with or without colour and literals) is not -/// narrowed to the cell width but written as `&` turns it into text: `124412140710`, not -/// `1.24412E+11`. Excel writes the cell display here; GridCraft keeps the digits. +/// narrowed to the cell width but written by `general_full`: `124412140710`, not `1.24412E+11`. +/// Excel writes the cell display here; GridCraft keeps the digits. pub fn format_value_full(v: &Value, fmt: &NumberFormat, sys: DateSystem) -> Formatted { format_value_with(v, fmt, sys, true) } +/// General for Save As CSV/Text: the text `&` gives (15 significant digits), but never with an +/// exponent, because reopening the file would give the cell a scientific format that rounds it +/// on the next save. Where `&` writes an exponent, a number of 1 or more is written as the +/// shortest digits that read back as the same value (every integer f64 holds below 1e16 exactly, +/// so 16-digit card numbers survive), and a smaller one as its 15 significant digits in full. +fn general_full(n: f64) -> String { + let text = gridcraft_core::number_to_text(n); + if !text.contains('E') { + text + } else if n.abs() >= 1.0 { + format!("{n}") + } else { + let mag: i32 = format!("{n:.14e}").split_once('e').and_then(|(_, e)| e.parse().ok()).unwrap_or(0); + gridcraft_core::value::trim_decimal(&format!("{:.*}", (14 - mag).max(0) as usize, n)) + } +} + fn format_value_with(v: &Value, fmt: &NumberFormat, sys: DateSystem, full: bool) -> Formatted { match v { Value::Empty => Formatted::default(), diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index 516130915..e8d4fde4a 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -1,6 +1,6 @@ # File-format parity -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-13 · **Change:** minor (CSV/text export writes every General number as `&` converts it) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-14 · **Change:** minor (CSV/text export writes every General number with `&`'s 15 digits, never an exponent; integers below 1e16 keep every digit) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Every format Excel opens or saves, with GridCraft's read and write support, fidelity and tests. Part of [target-app-parity.md](target-app-parity.md). **File formats: ~50% ready, 80–120 h** @@ -41,7 +41,7 @@ and repair-free opening remain unverified. | Format | Excel | GridCraft read | GridCraft write | Notes | |---|---|---|---|---| -| `.csv` UTF-8 / Windows / Mac / MS-DOS | R/W | yes (UTF-8 and UTF-16 BOMs, UTF-8, Windows-1252 fallback; delimiter detection) | yes (UTF-8 without BOM, CRLF; a file opened as CSV is saved with the separator it was read with, so a `;` file stays `;`; otherwise `,`) | Formatted numbers and formula results are written as displayed, as in Excel (never column-width `#####`). **Intentional difference:** Excel writes a General number as the cell displays it (`1.24412E+11`, `0.333333333`); GridCraft writes an integer below 2^53 (which f64 holds exactly) with all its digits (`124412140710`, `1234567890123456`), so part, card and order numbers survive a round trip, and any other number to 15 significant digits, as `&` converts it (`0.333333333333333`, `1.23456789012346E+16`). The same applies to a coloured or sectioned General (`[Blue]General`, `General;[Red]-General`) and to a number in a Text (`@`) cell. No encoding choice on save. Re-import of a single-column export splits text (#127). A used range that would write more than 512 MiB (two far-apart cells span the whole rectangle) is refused with an error instead of being expanded (#528). | +| `.csv` UTF-8 / Windows / Mac / MS-DOS | R/W | yes (UTF-8 and UTF-16 BOMs, UTF-8, Windows-1252 fallback; delimiter detection) | yes (UTF-8 without BOM, CRLF; a file opened as CSV is saved with the separator it was read with, so a `;` file stays `;`; otherwise `,`) | Formatted numbers and formula results are written as displayed, as in Excel (never column-width `#####`). **Intentional difference:** Excel writes a General number as the cell displays it (`1.24412E+11`, `0.333333333`); GridCraft writes it with the 15 significant digits `&` gives (`0.333333333333333`, `0.3`, not `0.30000000000000004`) but never with an exponent, since reopening the file would give the cell a scientific format that rounds it on the next save: a number of 1E+15 or more is written as the shortest digits that read back as the same value (every integer below 1e16 exactly, so 16-digit card and order numbers survive: `9007199254740993` is stored and written as `9007199254740992`, `12345678901234567` as `12345678901234568`), a small one as its 15 digits in full (`0.00000128233995888454`). A CSV opened and saved again is unchanged. The same applies to a coloured or sectioned General (`[Blue]General`, `General;[Red]-General`) and to a number in a Text (`@`) cell. No encoding choice on save. Re-import of a single-column export splits text (#127). A used range that would write more than 512 MiB (two far-apart cells span the whole rectangle) is refused with an error instead of being expanded (#528). | | `.txt` tab-delimited, Unicode text | R/W | yes (`.txt`, `.tsv`, `.tab`) | yes (displayed values, with General numbers as for `.csv`) | Text Import Wizard options partly via Text to Columns | | `.prn` formatted text (space-delimited) | R/W | no | no | | | `.dif`, `.slk` (SYLK) | R/W | no | no | Small, legacy | @@ -83,7 +83,7 @@ refresh. Counted under features (Get & Transform, ~10% ready), not here. | Date | Change | Summary | |---|---|---| -| 2026-10-13 | minor | CSV/text export writes General numbers as `&` converts them (15 significant digits: `0.3`, not `0.30000000000000004`; integers below 2^53 keep every digit) also through coloured or sectioned General and Text formats (`[Blue]General` still wrote `1.24412E+11`). Duplicate CSV/text rows merged. Intentional difference from Excel, which writes the cell display | +| 2026-10-14 | minor | CSV/text export writes General numbers with the 15 significant digits `&` gives (`0.3`, not `0.30000000000000004`) but never an exponent (integers below 1e16 keep every digit; 1E+15 and up as the shortest round-trip digits; small numbers written out), also through coloured or sectioned General and Text formats (`[Blue]General` still wrote `1.24412E+11`). Duplicate CSV/text rows merged. Intentional difference from Excel, which writes the cell display. The underlying `&` conversion changed too (see [function-parity.md](function-parity.md)) | | 2026-10-12 | trivial | XLSX chart sheets are kept on save (#727) | | 2026-10-12 | trivial | CSV and TSV export is capped at 512 MiB and refuses a larger used range with an error (#528) | | 2026-10-12 | minor | `.xls` import reads the calculation settings (CalcMode, CalcIter, CalcCount, CalcDelta, CalcPrecision), so circular models calculate instead of showing `#CIRC!` (#724) | From 9cbbaaeaed651824222e4a9f053cfb17e0c603dd Mon Sep 17 00:00:00 2001 From: Local contributor Date: Wed, 14 Oct 2026 13:51:54 -0400 Subject: [PATCH 6/8] Docs: CSV export wording matches the code; inferred & boundaries marked From 1E+15, CSV export writes the shortest round-trip digits, not &'s 15 digits. 'Every digit' now applies only to integers below 1e16. The open/save round-trip claim is narrowed to files GridCraft wrote. The doc now lists what a foreign CSV loses on its first save: 16-17-digit decimals are cut to 15 digits, and scientific input is rounded to its 0.00E+00 format. The 21-character switch is Excel-verified only for positive 15-digit mantissas. Shorter mantissas from 1E-6 to 1E-9 and negatives are marked as inferred, and the doc states the sign isn't counted. The function-parity 'was' value is corrected to main's 0.00000001. --- crates/core/src/value.rs | 7 +++++-- crates/engine/src/display.rs | 5 +++-- crates/engine/src/io.rs | 9 +++++---- docs/file-format-parity.md | 4 ++-- docs/function-parity.md | 2 +- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/crates/core/src/value.rs b/crates/core/src/value.rs index a84bdda61..ef4b910f9 100644 --- a/crates/core/src/value.rs +++ b/crates/core/src/value.rs @@ -351,8 +351,11 @@ pub fn sort_compare(a: &Value, b: &Value) -> Ordering { /// 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). +/// longer than 21 characters, not counting the sign (`0.` plus 19 decimals). Excel-verified only +/// for positive 15-digit mantissas: `0.0000692361330972509` stays decimal and +/// `1.28233995888454E-06` does not, as Excel caches them. The limit for shorter mantissas from +/// 1E-6 to 1E-9 (e.g. `0.0000099999999999999` decimal) and for negatives (sign excluded) are +/// inferred. pub fn number_to_text(n: f64) -> String { if n == 0.0 || !n.is_finite() { return "0".into(); diff --git a/crates/engine/src/display.rs b/crates/engine/src/display.rs index 3b891caef..e0af7b300 100644 --- a/crates/engine/src/display.rs +++ b/crates/engine/src/display.rs @@ -95,8 +95,9 @@ pub fn cell_text(wb: &Workbook, sheet: &Sheet, c: CellRef) -> String { /// A cell's value with its number format applied, independent of column width: no `#####` /// and no General narrowing (Save As CSV/Text writes this). Formulas give their result. A number /// shown as General (also through a coloured or sectioned General, or a Text format) is written -/// as [`format_value_full`] writes it: `&`'s 15 significant digits without an exponent, an integer -/// below 1e16 with every digit (`124412140710`, not `1.24412E+11`). Excel writes the cell display. +/// as [`format_value_full`] writes it, never with an exponent: `&`'s 15 significant digits below +/// 1E+15 (`124412140710`, not `1.24412E+11`), the shortest round-trip digits from 1E+15 (every +/// integer below 1e16 exactly). Excel writes the cell display. pub fn value_text(wb: &Workbook, sheet: &Sheet, c: CellRef) -> String { let style = wb.styles.get(sheet.style_id(c)); with_fill_space(format_value_full(&sheet.value(c), &parsed(style.num_fmt.as_str()), wb.date_system)) diff --git a/crates/engine/src/io.rs b/crates/engine/src/io.rs index b52c8cb90..ee2054e9a 100644 --- a/crates/engine/src/io.rs +++ b/crates/engine/src/io.rs @@ -231,10 +231,11 @@ pub fn source_dir(path: &str) -> Option { /// Save As CSV/Text writes "the text and values as they are displayed in cells" with "all /// characters in each cell" (Microsoft Support, *Excel formatting and features that are not /// transferred to other file formats*): number formats applied, never column-width `#####`. -/// One intentional difference: a number shown as General is not narrowed to the cell width but -/// written as `&` converts it (15 significant digits) without an exponent, and an integer below -/// 1e16 with every digit, so long identifiers survive a reopen and save -/// (`gridcraft_numfmt::format_value_full`). +/// One intentional difference: a number shown as General is not narrowed to the cell width and +/// never gets an exponent (`gridcraft_numfmt::format_value_full`). Below 1E+15 it is written as +/// `&` converts it (15 significant digits); from 1E+15 as the shortest digits that read back as +/// the same value (every integer below 1e16 exactly, so long identifiers survive); small numbers +/// that `&` writes in scientific notation as their 15 digits in full. fn csv(wb: &Workbook, sh: &Sheet, delimiter: u8) -> Result> { gridcraft_xlsx::write_csv(sh, delimiter, |c| crate::display::value_text(wb, sh, c)).map_err(|e| EngineError::Other(e.to_string())) } diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index e8d4fde4a..4167c02bf 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -1,6 +1,6 @@ # File-format parity -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-14 · **Change:** minor (CSV/text export writes every General number with `&`'s 15 digits, never an exponent; integers below 1e16 keep every digit) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-14 · **Change:** minor (CSV/text export writes General numbers without an exponent: `&`'s 15 digits below 1E+15, the shortest round-trip digits from 1E+15, so integers below 1e16 keep every digit) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Every format Excel opens or saves, with GridCraft's read and write support, fidelity and tests. Part of [target-app-parity.md](target-app-parity.md). **File formats: ~50% ready, 80–120 h** @@ -41,7 +41,7 @@ and repair-free opening remain unverified. | Format | Excel | GridCraft read | GridCraft write | Notes | |---|---|---|---|---| -| `.csv` UTF-8 / Windows / Mac / MS-DOS | R/W | yes (UTF-8 and UTF-16 BOMs, UTF-8, Windows-1252 fallback; delimiter detection) | yes (UTF-8 without BOM, CRLF; a file opened as CSV is saved with the separator it was read with, so a `;` file stays `;`; otherwise `,`) | Formatted numbers and formula results are written as displayed, as in Excel (never column-width `#####`). **Intentional difference:** Excel writes a General number as the cell displays it (`1.24412E+11`, `0.333333333`); GridCraft writes it with the 15 significant digits `&` gives (`0.333333333333333`, `0.3`, not `0.30000000000000004`) but never with an exponent, since reopening the file would give the cell a scientific format that rounds it on the next save: a number of 1E+15 or more is written as the shortest digits that read back as the same value (every integer below 1e16 exactly, so 16-digit card and order numbers survive: `9007199254740993` is stored and written as `9007199254740992`, `12345678901234567` as `12345678901234568`), a small one as its 15 digits in full (`0.00000128233995888454`). A CSV opened and saved again is unchanged. The same applies to a coloured or sectioned General (`[Blue]General`, `General;[Red]-General`) and to a number in a Text (`@`) cell. No encoding choice on save. Re-import of a single-column export splits text (#127). A used range that would write more than 512 MiB (two far-apart cells span the whole rectangle) is refused with an error instead of being expanded (#528). | +| `.csv` UTF-8 / Windows / Mac / MS-DOS | R/W | yes (UTF-8 and UTF-16 BOMs, UTF-8, Windows-1252 fallback; delimiter detection) | yes (UTF-8 without BOM, CRLF; a file opened as CSV is saved with the separator it was read with, so a `;` file stays `;`; otherwise `,`) | Formatted numbers and formula results are written as displayed, as in Excel (never column-width `#####`). **Intentional difference:** Excel writes a General number as the cell displays it (`1.24412E+11`, `0.333333333`); GridCraft writes it with the 15 significant digits `&` gives (`0.333333333333333`, `0.3`, not `0.30000000000000004`) but never with an exponent, since reopening the file would give the cell a scientific format that rounds it on the next save: a number of 1E+15 or more is written as the shortest digits that read back as the same value (every integer below 1e16 exactly, so 16-digit card and order numbers survive: `9007199254740993` is stored and written as `9007199254740992`, `12345678901234567` as `12345678901234568`), a small one as its 15 digits in full (`0.00000128233995888454`). A CSV GridCraft wrote, opened and saved again, is unchanged. A CSV from elsewhere can change on its first save: a decimal with 16 or 17 significant digits is cut to 15, as in Excel (`3.141592653589793` → `3.14159265358979`), and a number typed in scientific notation gets the `0.00E+00` format on open and is saved rounded to it (`1.23456789012346E-10` → `1.23E-10`, unchanged from before). The same applies to a coloured or sectioned General (`[Blue]General`, `General;[Red]-General`) and to a number in a Text (`@`) cell. No encoding choice on save. Re-import of a single-column export splits text (#127). A used range that would write more than 512 MiB (two far-apart cells span the whole rectangle) is refused with an error instead of being expanded (#528). | | `.txt` tab-delimited, Unicode text | R/W | yes (`.txt`, `.tsv`, `.tab`) | yes (displayed values, with General numbers as for `.csv`) | Text Import Wizard options partly via Text to Columns | | `.prn` formatted text (space-delimited) | R/W | no | no | | | `.dif`, `.slk` (SYLK) | R/W | no | no | Small, legacy | diff --git a/docs/function-parity.md b/docs/function-parity.md index b0bd968ed..ce3dcaffb 100644 --- a/docs/function-parity.md +++ b/docs/function-parity.md @@ -108,7 +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-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 (sign not counted), as Excel caches IMCSCH/IMEXP/IMTAN results: `1.28233995888454E-06`, so `9.99999999999999E-09` (was `0.00000001`), but `0.0000692361330972509`. Only 15-digit positive mantissas are Excel-verified; the limit for shorter mantissas and for negatives is inferred | | 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 | From 3d19fa43f6e5fe4e1a51bebf06f02758ae730a56 Mon Sep 17 00:00:00 2001 From: Neil Cawse Date: Tue, 20 Oct 2026 03:14:18 +0000 Subject: [PATCH 7/8] docs: restore the XLSX right-to-left text box row lost in the main merge --- docs/file-format-parity.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index d7d6e30b2..88edb0882 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -85,6 +85,7 @@ refresh. Counted under features (Get & Transform, ~10% ready), not here. |---|---|---| | 2026-10-19 | minor | .xls import skips dialog sheets (WsBool `fDialog`), which share the worksheet BOF type and were read as empty worksheets | | 2026-10-19 | minor | `.xls` import reads WINDOW2: right-to-left sheets (common in older Arabic and Hebrew files) open right-to-left, and hidden gridlines, headings and zero values and Show Formulas carry over | +| 2026-10-19 | trivial | XLSX text boxes and shapes keep right-to-left paragraph direction (`a:pPr rtl`, ECMA-376 Part 1 §21.1.2.2.7): read from the first paragraph and written on every paragraph; output without it is unchanged | | 2026-10-19 | minor | HTML (Save as Web Page and clipboard) marks right-to-left sheets and right-to-left cell text with `dir` | | 2026-10-18 | trivial | `file.open {csvAsText: true}` keeps every CSV/TSV/TXT field as the text the file holds, so identifiers (GTINs, SKUs, ZIP codes) keep their digits and leading zeros (#1808; a preference and a CLI flag are separate work) | | 2026-10-18 | trivial | XLSX PivotTables: `rowGrandTotals` is the Grand Total row and `colGrandTotals` the Grand Total column. Both were read and written the other way round, which a GridCraft round trip hid, so an Excel file with only one grand total opened showing the other | From 77cfbda1bc0f9784e1f05b82154b7919d62b3c87 Mon Sep 17 00:00:00 2001 From: Neil Cawse Date: Thu, 22 Oct 2026 05:18:54 +0000 Subject: [PATCH 8/8] docs: order parity revision history --- docs/file-format-parity.md | 2 +- docs/function-parity.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index f43c80159..fa9df6c0b 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -117,9 +117,9 @@ refresh. Counted under features (Get & Transform, ~10% ready), not here. | 2026-10-19 | minor | .xls import skips dialog sheets (WsBool `fDialog`), which share the worksheet BOF type and were read as empty worksheets | | 2026-10-19 | minor | `.xls` import reads WINDOW2: right-to-left sheets (common in older Arabic and Hebrew files) open right-to-left, and hidden gridlines, headings and zero values and Show Formulas carry over | | 2026-10-19 | trivial | XLSX text boxes and shapes keep right-to-left paragraph direction (`a:pPr rtl`, ECMA-376 Part 1 §21.1.2.2.7): read from the first paragraph and written on every paragraph; output without it is unchanged | +| 2026-10-19 | minor | HTML (Save as Web Page and clipboard) marks right-to-left sheets and right-to-left cell text with `dir` | | 2026-10-18 | trivial | `file.open {csvAsText: true}` keeps every CSV/TSV/TXT field as the text the file holds, so identifiers (GTINs, SKUs, ZIP codes) keep their digits and leading zeros (#1808; a preference and a CLI flag are separate work) | | 2026-10-18 | trivial | XLSX PivotTables: `rowGrandTotals` is the Grand Total row and `colGrandTotals` the Grand Total column. Both were read and written the other way round, which a GridCraft round trip hid, so an Excel file with only one grand total opened showing the other | -| 2026-10-19 | minor | HTML (Save as Web Page and clipboard) marks right-to-left sheets and right-to-left cell text with `dir` | | 2026-10-16 | trivial | XLSX charts: `[0]!Name` (a workbook-scoped defined name) in series references is read as the name, so the series resolve instead of showing `#NAME?`. Names in chart references are written qualified (`[0]!Name`, or `Sheet!Name` for a sheet-scoped name): Excel draws a chart with a bare name blank (#1686) | | 2026-10-16 | trivial | XLSX reads and writes the workbook's show-objects setting (`workbookPr showObjects`: all, placeholders, none), which Ctrl+6 switches; a file without it is saved without it | | 2026-10-16 | trivial | XLSX: pictures in headers and footers (`&G`, the VML drawing `` names and its images) are kept on save; they are still not shown (#1257) | diff --git a/docs/function-parity.md b/docs/function-parity.md index 74916759c..6470b9936 100644 --- a/docs/function-parity.md +++ b/docs/function-parity.md @@ -128,8 +128,8 @@ workbooks; results checked in as data, not Excel files) and fixing what it finds | 2026-10-17 | minor | 15-significant-digit comparison moved from missing to done (#57), and the resolved caveats on the 3-D reference, dynamic-array and legacy-array rows cleared (#140, #133, #139, #156) | | 2026-10-16 | minor | FORECAST.ETS gap interpolation uses a forward-only known-point scan instead of rescanning each gap for every missing value; numerical behavior and missing-point limits are preserved (#1042) | | 2026-10-15 | minor | MEDIAN and AGGREGATE median use an overflow-safe midpoint (#1579); regressions cover finite extremes, tiny values and dependent recalculation | -| 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 (sign not counted), as Excel caches IMCSCH/IMEXP/IMTAN results: `1.28233995888454E-06`, so `9.99999999999999E-09` (was `0.00000001`), but `0.0000692361330972509`. Only 15-digit positive mantissas are Excel-verified; the limit for shorter mantissas and for negatives is inferred | | 2026-10-15 | minor | ADDRESS quotes a sheet name the way Excel does: names that read as an A1 cell (`A1`) or an R1C1 reference (`R1C1`, `RC`) and names starting with `.` get quotes; `TRUE` / `FALSE` stay bare (#1458) | +| 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 (sign not counted), as Excel caches IMCSCH/IMEXP/IMTAN results: `1.28233995888454E-06`, so `9.99999999999999E-09` (was `0.00000001`), but `0.0000692361330972509`. Only 15-digit positive mantissas are Excel-verified; the limit for shorter mantissas and for negatives is inferred | | 2026-10-13 | minor | Clear reports changed stored cells sparsely, including those beyond the first million coordinates; dependent formulas and removed spills update, while clearing empty merges still unblocks spills (#1032) | | 2026-10-14 | minor | Bounded references up to 1,048,576 cells (`D3:AZ400`) keep their full shape instead of stopping at the used range, so `SUMPRODUCT(D3:AZ400*B3:B400,(A3:A400=x)*(D1:AZ1=y))` broadcasts like Excel and `ROWS`, `COUNTBLANK` and `--(r="")` count the blanks; references that run to the edge of the sheet (`A:A`) still stop at the used range (#1010) | | 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) |