Skip to content

fix(xlsx): formatValue matches Excel for fractions, times, empty sections, [mm] and rounding - #16

Merged
pierreneter merged 1 commit into
p10node:mainfrom
mkroemer:fix-numfmt-excel
Nov 17, 2026
Merged

pierreneter merged 1 commit into
p10node:mainfrom
mkroemer:fix-numfmt-excel

Conversation

@mkroemer

Copy link
Copy Markdown
Contributor

This fixes five places where formatValue in src/core/xlsx/numfmt.ts gave different results from Excel. Each repro from the issue now has a test.

Negative fractions no longer get two minus signs. formatFraction formats only the magnitude and formatValue adds the sign, so -1.5 with # ?/? gives -1 1/2.

Times are rounded to the smallest unit the format shows (whole seconds, or the digits after .0) before being split into hours, minutes and seconds, the way TEXT() in formula/dates.ts already does. 0.00001 with h:mm:ss gives 0:00:01 and 0.12345 with mm:ss.0 gives 57:46.1.

An empty third section now shows zero as an empty string, so 0;-0;;@ on 0 gives an empty string. TEXT() in formula/format.ts had the same fallback to the first section; both now use one helper, pickSection, which sits next to roundHalf.

[hh], [mm] and [ss] are padded to their width, so [mm]:ss on 0.001 gives 01:26.

Fixed decimals are rounded with the existing roundHalf, which rounds the 15-significant-digit decimal value, instead of toFixed on the raw double. 2.675 and 1.005 with 0.00 now give 2.68 and 1.01.

Fixes #3

🤖 Generated with Claude Code

- Negative fractions no longer get two minus signs: formatFraction now
  formats the magnitude only and formatValue adds the sign.
- Times are rounded to the smallest unit shown (seconds, or the digits
  of ".0") before being split into fields, as TEXT() already does.
- An empty third section shows zero as an empty string. The section
  choice is shared with TEXT() through pickSection, which had the same
  fallback to the first section.
- [hh], [mm] and [ss] are padded to their width.
- Fixed decimals round the 15-digit decimal value with roundHalf
  instead of calling toFixed on the raw double (2.675 -> 2.68).

Fixes #3

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pierreneter

Copy link
Copy Markdown
Member

@mkroemer thanks for the contributions! I'll review them tomorrow.

@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.

Thank you for the fix, and for a test per repro from #3.

Approved. I ran 40 more formats against main: 11 change, all towards what Excel shows (-2.675 -> -2.68, 9.995 as 0.00E+00 -> 1.00E+01, 0 with 0;; -> empty), and the rest are identical.

Two edge cases for a follow-up, neither blocking:

  • A date-only format within half a second of midnight now shows the next day (0.99999999 as m/d/yyyy), because the time is rounded to seconds even when the format shows none. Worth checking against Excel.
  • 1e21 as 0.00 gives 999999999999999868928.00. main gives 1e+21., so it was broken before too.

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.

formatValue: negative fractions, time rounding, empty zero section, [mm] padding, and decimal rounding differ from Excel

3 participants