Skip to content

fix(xlsx): render large fixed decimals without binary artifacts - #24

Open
mkroemer wants to merge 1 commit into
p10node:mainfrom
mkroemer:fix/numfmt-large-fixed-decimals
Open

mkroemer wants to merge 1 commit into
p10node:mainfrom
mkroemer:fix/numfmt-large-fixed-decimals

Conversation

@mkroemer

Copy link
Copy Markdown
Contributor

Fixed formats such as 0.00 now display 1e21 as 1000000000000000000000.00, without binary artifacts or unintended exponential notation. Rendering retains 15 significant decimal digits through rounding and expansion.

Follows up on #16. Reference checks in Excel 16.115 confirmed that date-only formats carry across midnight after rounding to seconds in both date systems. That behavior is preserved, with regression coverage and a corrected explanatory comment.

Tests cover neighboring and negative large values, grouping/scaling, percentages, scientific/fraction formats, precision, and date boundaries. Existing serial-zero and negative-time behavior remains unchanged. Extreme finite values are unit-tested, but exact Excel parity at those magnitudes is not claimed.

Validation with Bun 1.4.4: frozen dependencies, all 544 unit tests, typecheck, full build, and diff whitespace checks passed. No browser suite was rerun for this pure number-format change.

@pierreneter pierreneter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the core holds: fixedDecimal matches the old output at ordinary magnitudes (|v| < 1e8, up to 6 decimals, 4M differential checks), and the new tests pass.

A few things around it. Outputs below were run on this branch; line numbers are at 6e3735e.

  1. src/react/xlsx/fit.ts:12: fitNumber re-parses the text with Number(text).toFixed(keep), so the grid and print bring back what this PR removes as soon as the column is 1-3 characters too narrow. 1e21 with 0.00 is 1000000000000000000000.00 at full width and 1e+21 one notch narrower. For 1e21 - 1e7 the digits change while the column is resized: 999999999999990000000.00, then 999999999999990038528.0.
  2. Speed on the hot path:
    • src/core/xlsx/numfmt.ts:211: formatValue with format 0 on 2M values takes about 1.08 s here against 0.65 s before. The coefficient has at most 15 digits and is exact in a double, so BigInt is not needed; a Number-only variant gave the same output on 3M fuzzed inputs.
    • numfmt.ts:199: group() is a backtracking lookahead and now gets integer strings of up to 309 digits. formatValue(1e308, '#,##0.00') went from about 0.5 µs to 75 µs per call. A slice-by-3 loop is linear.
  3. src/core/xlsx/formula/format.ts:71: TEXT() still goes through roundHalf(...).toFixed(decs), so the formula and the cell format now disagree for the same value: =TEXT(1E+22,"0.00") gives 1e+22., a cell formatted 0.00 shows all the digits.

Non-blocking, but the description lists these as covered:

  • Fractions: formatValue(1e21, '# ?/?') is still 1e+21 (numfmt.ts:362).
  • Percent is scaled in binary before the conversion: formatValue(1e307, '0%') gives Infinity% (numfmt.ts:223). Folding the scale into the exponent shift avoids it.
  • Scientific: formatValue(5e-324, '0.00E+00') gives Infinity.E-323 (numfmt.ts:251).
  • fixedDecimal repeats the digit / exponent split that numToText already has (src/core/xlsx/formula/values.ts:59-73). One shared helper would also close the TEXT() gap.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants