From 2c277e4eed44edb3dfba8b0d6e6fc1da5fba37f9 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 31 Jul 2026 04:13:02 +0000 Subject: [PATCH 1/3] fix(node:util): correct numericSeparator for negative fractions and exponent-form numbers util.inspect(n, { numericSeparator: true }) mangled negative fractional numbers and numbers that stringify in exponent form: -0.12 -> "0..12" (sign dropped, doubled decimal point) 1e-7 -> "0.-7" 1.23456e-7 -> "0.234_56e_-7" -0 -> "0" (sign dropped) formatNumber derived the fractional slice from String(Math.trunc(n)).length. For -0.12, Math.trunc is -0 and String(-0) is "0", so the sign was lost and the slice offset was wrong. For 1e-7, trunc is 0 so "0" bore no relation to the actual "." position in "1e-7". Port Node's current formatNumber: hoist the -0 check above the separator branch, split the number's own string on indexOf("."), and pass exponent-form strings through unchanged. Closes #23098 Adopted from #32460. Co-authored-by: 0xfandom --- src/js/internal/util/inspect.js | 33 +++++++++++++---------- test/js/node/util/util.test.js | 47 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 14 deletions(-) diff --git a/src/js/internal/util/inspect.js b/src/js/internal/util/inspect.js index 09d55ecafcbe..414189d9b2ac 100644 --- a/src/js/internal/util/inspect.js +++ b/src/js/internal/util/inspect.js @@ -2205,28 +2205,33 @@ function addNumericSeparatorEnd(integerString) { const remainingText = remaining => `... ${remaining} more item${remaining > 1 ? "s" : ""}`; function formatNumber(fn, number, numericSeparator) { + // Format -0 as '-0'. Checking `number === -0` won't distinguish 0 from -0. + // String(-0) === '0', so this must be checked before any String() conversion. + if (ObjectIs(number, -0)) { + return fn("-0", "number"); + } if (!numericSeparator) { - // Format -0 as '-0'. Checking `number === -0` won't distinguish 0 from -0. - if (ObjectIs(number, -0)) { - return fn("-0", "number"); - } return fn(`${number}`, "number"); } + + const numberString = String(number); const integer = MathTrunc(number); - const string = String(integer); + if (integer === number) { - if (!NumberIsFinite(number) || StringPrototypeIncludes(string, "e")) { - return fn(string, "number"); + if (!NumberIsFinite(number) || StringPrototypeIncludes(numberString, "e")) { + return fn(numberString, "number"); } - return fn(`${addNumericSeparator(string)}`, "number"); + return fn(addNumericSeparator(numberString), "number"); } - if (NumberIsNaN(number)) { - return fn(string, "number"); + if (NumberIsNaN(number) || StringPrototypeIncludes(numberString, "e")) { + return fn(numberString, "number"); } - return fn( - `${addNumericSeparator(string)}.${addNumericSeparatorEnd(StringPrototypeSlice(String(number), string.length + 1))}`, - "number", - ); + + const decimalIndex = StringPrototypeIndexOf(numberString, "."); + const integerPart = StringPrototypeSlice(numberString, 0, decimalIndex); + const fractionalPart = StringPrototypeSlice(numberString, decimalIndex + 1); + + return fn(`${addNumericSeparator(integerPart)}.${addNumericSeparatorEnd(fractionalPart)}`, "number"); } function formatBigInt(fn, bigint, numericSeparator) { diff --git a/test/js/node/util/util.test.js b/test/js/node/util/util.test.js index 7689c1230321..33116ab877fb 100644 --- a/test/js/node/util/util.test.js +++ b/test/js/node/util/util.test.js @@ -462,6 +462,53 @@ describe("util", () => { }); }); + describe("inspect numericSeparator", () => { + const sep = v => util.inspect(v, { numericSeparator: true }); + + it("formats negative fractional numbers (#23098)", () => { + // Previously produced "0..12" and dropped the sign. + expect(sep([0.1234, -0.12, -0.123, -0.1234, -1.234])).toBe("[ 0.123_4, -0.12, -0.123, -0.123_4, -1.234 ]"); + }); + + it("matches Node for each negative fraction", () => { + expect(sep(-0.12)).toBe("-0.12"); + expect(sep(-0.123)).toBe("-0.123"); + expect(sep(-0.1234)).toBe("-0.123_4"); + expect(sep(-1.234)).toBe("-1.234"); + expect(sep(-12345.6789)).toBe("-12_345.678_9"); + }); + + it("keeps the sign on -0", () => { + expect(sep(-0)).toBe("-0"); + expect(sep(0)).toBe("0"); + }); + + it("groups large integers and fractions", () => { + expect(sep(1234567)).toBe("1_234_567"); + expect(sep(-1234567)).toBe("-1_234_567"); + expect(sep(123456.789)).toBe("123_456.789"); + expect(sep(0.1234)).toBe("0.123_4"); + }); + + it("leaves exponent-form numbers intact", () => { + // The old integer-length split corrupted these: 1e-7 became "0.-7", + // -1e-7 lost its sign as "0.e-7", and 1.23456e-7 became "0.234_56e_-7". + expect(sep(1e-7)).toBe("1e-7"); + expect(sep(-1e-7)).toBe("-1e-7"); + expect(sep(2e-7)).toBe("2e-7"); + expect(sep(1.23456e-7)).toBe("1.23456e-7"); + expect(sep(-0.5)).toBe("-0.5"); + // Integers large enough to stringify in exponent form take the same path. + expect(sep(1234567891234567891234)).toBe("1.234567891234568e+21"); + }); + + it("handles non-finite values", () => { + expect(sep(NaN)).toBe("NaN"); + expect(sep(Infinity)).toBe("Infinity"); + expect(sep(-Infinity)).toBe("-Infinity"); + }); + }); + describe("getSystemErrorName", () => { for (const item of ["test", {}, []]) { it(`throws when passing: ${item}`, () => { From 9952f31d04455d4fece209c3a1000c2b05e6279e Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 31 Jul 2026 04:37:28 +0000 Subject: [PATCH 2/3] ci: retrigger From 810089971de5e35d19e02eacf413885c780c6a20 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 31 Jul 2026 05:03:10 +0000 Subject: [PATCH 3/3] test: drop explanatory comments; test names already reference #23098 --- test/js/node/util/util.test.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/js/node/util/util.test.js b/test/js/node/util/util.test.js index 33116ab877fb..47e816bb8a36 100644 --- a/test/js/node/util/util.test.js +++ b/test/js/node/util/util.test.js @@ -466,7 +466,6 @@ describe("util", () => { const sep = v => util.inspect(v, { numericSeparator: true }); it("formats negative fractional numbers (#23098)", () => { - // Previously produced "0..12" and dropped the sign. expect(sep([0.1234, -0.12, -0.123, -0.1234, -1.234])).toBe("[ 0.123_4, -0.12, -0.123, -0.123_4, -1.234 ]"); }); @@ -491,14 +490,11 @@ describe("util", () => { }); it("leaves exponent-form numbers intact", () => { - // The old integer-length split corrupted these: 1e-7 became "0.-7", - // -1e-7 lost its sign as "0.e-7", and 1.23456e-7 became "0.234_56e_-7". expect(sep(1e-7)).toBe("1e-7"); expect(sep(-1e-7)).toBe("-1e-7"); expect(sep(2e-7)).toBe("2e-7"); expect(sep(1.23456e-7)).toBe("1.23456e-7"); expect(sep(-0.5)).toBe("-0.5"); - // Integers large enough to stringify in exponent form take the same path. expect(sep(1234567891234567891234)).toBe("1.234567891234568e+21"); });