mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-18 16:14:57 +00:00
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.
This commit is contained in:
1 parent
6ff543ce76
commit
bf9b3cb9b1
4 files changed
+59
-35
No files matched your search
@@ -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))
|
||||
|
||||
+32
-15
@@ -231,8 +231,10 @@ pub fn source_dir(path: &str) -> Option<std::path::PathBuf> {
|
||||
/// 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<Vec<u8>> {
|
||||
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",
|
||||
|
||||
+21
-15
@@ -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(),
|
||||
|
||||
@@ -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) |
|
||||
|
||||
Reference in new issue
Block a user