From 167e59e75447653da59b16d19a5cc8cff5a323e5 Mon Sep 17 00:00:00 2001 From: Aman Sachan Date: Mon, 27 Apr 2026 20:14:50 +0000 Subject: [PATCH 1/2] util.inspect: fix numericSeparator for scientific notation numbers When numericSeparator: true is set, util.inspect produces corrupted output like '1e-.1e-_7' for numbers in scientific notation. The fix adds an early return in formatNumber() for scientific notation numbers (containing 'e') before the decimal-split + numeric separator logic runs. Fixes nodejs/node#62981 --- lib/internal/util/inspect.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index c611eb97fc0755..c1ce537d379e65 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -2210,6 +2210,11 @@ function formatNumber(fn, number, numericSeparator) { return fn(numberString, 'number'); } + // Scientific notation cannot have numeric separators applied sensibly + if (StringPrototypeIncludes(numberString, 'e')) { + return fn(numberString, 'number'); + } + const decimalIndex = StringPrototypeIndexOf(numberString, '.'); const integerPart = StringPrototypeSlice(numberString, 0, decimalIndex); const fractionalPart = StringPrototypeSlice(numberString, decimalIndex + 1); From 4e6af9032a4e93bd38f4615676fd5dbbf794e93f Mon Sep 17 00:00:00 2001 From: Aman Sachan Date: Mon, 27 Apr 2026 21:48:33 +0000 Subject: [PATCH 2/2] util.inspect: fix numericSeparator for scientific notation numbers Move the scientific notation check to the start of formatNumber() to fix corrupted output like '1e-.1e-_7' instead of '1e-7'. Previously the check was placed AFTER integer === number which caused it to be checked too late in the flow, allowing scientific notation numbers to reach the numeric separator logic incorrectly. Also adds comprehensive test cases for scientific notation with numericSeparator: true covering various exponent formats. Fixes nodejs#62981 --- lib/internal/util/inspect.js | 13 ++++++----- test/parallel/test-util-inspect.js | 37 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index c1ce537d379e65..61374d49ec09a3 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -2198,10 +2198,16 @@ function formatNumber(fn, number, numericSeparator) { } const numberString = String(number); + + // Scientific notation cannot have numeric separators applied sensibly + if (StringPrototypeIncludes(numberString, 'e')) { + return fn(numberString, 'number'); + } + const integer = MathTrunc(number); if (integer === number) { - if (!NumberIsFinite(number) || StringPrototypeIncludes(numberString, 'e')) { + if (!NumberIsFinite(number)) { return fn(numberString, 'number'); } return fn(addNumericSeparator(numberString), 'number'); @@ -2210,11 +2216,6 @@ function formatNumber(fn, number, numericSeparator) { return fn(numberString, 'number'); } - // Scientific notation cannot have numeric separators applied sensibly - if (StringPrototypeIncludes(numberString, 'e')) { - return fn(numberString, 'number'); - } - const decimalIndex = StringPrototypeIndexOf(numberString, '.'); const integerPart = StringPrototypeSlice(numberString, 0, decimalIndex); const fractionalPart = StringPrototypeSlice(numberString, decimalIndex + 1); diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index e60320d0591233..4e83ffe6b49ea2 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -3846,6 +3846,43 @@ assert.strictEqual( } } +// Regression test for https://github.com/nodejs/node/issues/62981 +// numericSeparator should not corrupt scientific notation numbers +{ + // Test cases from the bug report + assert.strictEqual( + util.inspect(1e-7, { numericSeparator: true }), + '1e-7' + ); + assert.strictEqual( + util.inspect(1.23e-7, { numericSeparator: true }), + '1.23e-7' + ); + assert.strictEqual( + util.inspect(1.23e+7, { numericSeparator: true }), + '1.23e+7' + ); + assert.strictEqual( + util.inspect(-1.23e-7, { numericSeparator: true }), + '-1.23e-7' + ); + assert.strictEqual( + util.inspect(1e10, { numericSeparator: true }), + '1e10' + ); + assert.strictEqual( + util.inspect(1.2345678901234567e+20, { numericSeparator: true }), + '1.2345678901234567e+20' + ); + + // With default numericSeparator: true + const { numericSeparator } = util.inspect.defaultOptions; + util.inspect.defaultOptions.numericSeparator = true; + assert.strictEqual(util.inspect(1e-7), '1e-7'); + assert.strictEqual(util.inspect(1.5e-10), '1.5e-10'); + util.inspect.defaultOptions.numericSeparator = numericSeparator; +} + // Regression test for https://github.com/nodejs/node/issues/41244 { assert.strictEqual(util.inspect({