From e476e43c178ae13f92274b53ceb2cbf6ba6dfab9 Mon Sep 17 00:00:00 2001 From: sangwook <73056306+Han5991@users.noreply.github.com> Date: Tue, 9 Sep 2025 05:10:50 +0900 Subject: [PATCH] util: fix numericSeparator with negative fractional numbers Fix util.inspect() formatting bug where negative fractional numbers between -1 and 0 lost their minus sign when numericSeparator was true. Fixed formatNumber function to preserve sign by using original string representation. Also corrected test expectations for scientific notation to not apply numeric separators. Fixes: https://github.com/nodejs/node/issues/59376 PR-URL: https://github.com/nodejs/node/pull/59379 Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell --- lib/internal/util/inspect.js | 23 ++++++++++++++--------- test/parallel/test-util-inspect.js | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index adbf5b72fa1..d7c8539fbfc 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1851,23 +1851,28 @@ function formatNumber(fn, number, numericSeparator) { } 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'); + return fn(numberString, 'number'); } + + const decimalIndex = StringPrototypeIndexOf(numberString, '.'); + const integerPart = StringPrototypeSlice(numberString, 0, decimalIndex); + const fractionalPart = StringPrototypeSlice(numberString, decimalIndex + 1); + return fn(`${ - addNumericSeparator(string) + addNumericSeparator(integerPart) }.${ - addNumericSeparatorEnd( - StringPrototypeSlice(String(number), string.length + 1), - ) + addNumericSeparatorEnd(fractionalPart) }`, 'number'); } diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 054526268a0..22aa57612a3 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -3493,6 +3493,27 @@ assert.strictEqual( util.inspect(-123456789.12345678, { numericSeparator: true }), '-123_456_789.123_456_78' ); + + // Regression test for https://github.com/nodejs/node/issues/59376 + // numericSeparator should work correctly for negative fractional numbers + { + // Test the exact values from the GitHub issue + const values = [0.1234, -0.12, -0.123, -0.1234, -1.234]; + assert.strictEqual( + util.inspect(values, { numericSeparator: true }), + '[ 0.123_4, -0.12, -0.123, -0.123_4, -1.234 ]' + ); + + // Test individual negative fractional numbers between -1 and 0 + assert.strictEqual( + util.inspect(-0.1234, { numericSeparator: true }), + '-0.123_4' + ); + assert.strictEqual( + util.inspect(-0.12345, { numericSeparator: true }), + '-0.123_45' + ); + } } // Regression test for https://github.com/nodejs/node/issues/41244