Skip to content

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

Closed

Description

@trackoor

formatValue in src/core/xlsx/numfmt.ts (used to format XLSX cells and exported from @p10node/office-engine/core) differs from Excel on negative fractions, sub-second time rounding, empty zero sections, padded elapsed time tokens, and half-way decimal rounding.

npm i @p10node/office-engine@0.13.2
node --input-type=module -e '
import { formatValue } from "@p10node/office-engine/core";
const f = (v, fmt) => formatValue(v, fmt).text;

console.log(f(-1.5, "# ?/?"));      // "--1 1/2"   expected: "-1 1/2"
console.log(f(-0.5, "?/?"));        // "--1/2"     expected: "-1/2"
console.log(f(0.00001, "h:mm:ss")); // "0:00:00"   expected: "0:00:01"
console.log(f(99.999, "h:mm:ss"));  // "23:58:33"  expected: "23:58:34"
console.log(f(0.12345, "mm:ss.0")); // "57:46.0"   expected: "57:46.1"
console.log(f(0, "0;-0;;@"));       // "0"         expected: ""
console.log(f(0.001, "[mm]:ss"));   // "1:26"      expected: "01:26"
console.log(f(2.675, "0.00"));      // "2.67"      expected: "2.68"
console.log(f(1.005, "0.00"));      // "1.00"      expected: "1.01"
'
  1. Negative fractions get two minus signs (--1 1/2, --1/2): formatFraction already prepends - when v < 0, and formatValue prepends - again for val < 0.
  2. Times truncate instead of rounding to the smallest displayed unit (0:00:00 instead of 0:00:01 for 0.864s, 57:46.0 instead of 57:46.1 for 57m 46.08s): formatDate reads getUTCSeconds() and Math.floor(msec / ...) rather than rounding, unlike TEXT() in src/core/xlsx/formula/dates.ts (which returns "0:00:01" and "23:58:34").
  3. An empty third section (0;-0;;@ or #,##0;(#,##0);) displays "0" instead of "" (Excel number format codes): formatValue guards section 3 with secs[2].toks.length (and TEXT() in src/core/xlsx/formula/format.ts similarly checks sections[2] !== ''), falling back to section 1.
  4. [hh], [mm], and [ss] ignore token width (1:26 instead of 01:26): parseSection discards the bracketed token length and formatDate writes Math.floor(...) without padStart, unlike TEXT() in src/core/xlsx/formula/dates.ts (which returns "01:26").
  5. Fixed decimals call mant.toFixed(decCount) on the raw IEEE-754 double (2.67 and 1.00 instead of 2.68 and 1.01), whereas Excel rounds the 15-significant-digit decimal value as roundHalf in src/core/xlsx/formula/format.ts already does (TEXT(2.675, "0.00") returns "2.68" and TEXT(1.005, "0.00") returns "1.01").

Version: @p10node/office-engine@0.13.2, also on main (715d2aa), Node.js v26.13.0.

Credit: https://ucsbplse.github.io/

Activity

  1. pierreneter commented on Nov 17, 2026

    @pierreneter
    Member

    Thanks for the report, and for the exact repros. They made this easy to check, and each one is now a test.

    All five cases are fixed on main by #16 (thanks to mkroemer for the fix): negative fractions, time rounding, the empty zero section, padded [mm] and half-way decimal rounding. It will be in the next release after v0.13.2.

    If you find another format that differs from Excel, please open a new issue.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions