Skip to content

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

Open
mkroemer wants to merge 1 commit into
p10node:mainfrom
mkroemer:fix-numfmt-excel
Open

mkroemer wants to merge 1 commit into
p10node:mainfrom
mkroemer:fix-numfmt-excel

Conversation

@mkroemer

Copy link
Copy Markdown

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>

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.

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

2 participants