Skip to content

Commit 670d7ab

Browse files
authored
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: #59376 PR-URL: #59379 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 26607a6 commit 670d7ab

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

lib/internal/util/inspect.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,23 +2103,28 @@ function formatNumber(fn, number, numericSeparator) {
21032103
}
21042104
return fn(`${number}`, 'number');
21052105
}
2106+
2107+
const numberString = String(number);
21062108
const integer = MathTrunc(number);
2107-
const string = String(integer);
2109+
21082110
if (integer === number) {
2109-
if (!NumberIsFinite(number) || StringPrototypeIncludes(string, 'e')) {
2110-
return fn(string, 'number');
2111+
if (!NumberIsFinite(number) || StringPrototypeIncludes(numberString, 'e')) {
2112+
return fn(numberString, 'number');
21112113
}
2112-
return fn(`${addNumericSeparator(string)}`, 'number');
2114+
return fn(addNumericSeparator(numberString), 'number');
21132115
}
21142116
if (NumberIsNaN(number)) {
2115-
return fn(string, 'number');
2117+
return fn(numberString, 'number');
21162118
}
2119+
2120+
const decimalIndex = StringPrototypeIndexOf(numberString, '.');
2121+
const integerPart = StringPrototypeSlice(numberString, 0, decimalIndex);
2122+
const fractionalPart = StringPrototypeSlice(numberString, decimalIndex + 1);
2123+
21172124
return fn(`${
2118-
addNumericSeparator(string)
2125+
addNumericSeparator(integerPart)
21192126
}.${
2120-
addNumericSeparatorEnd(
2121-
StringPrototypeSlice(String(number), string.length + 1),
2122-
)
2127+
addNumericSeparatorEnd(fractionalPart)
21232128
}`, 'number');
21242129
}
21252130

test/parallel/test-util-inspect.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3493,6 +3493,27 @@ assert.strictEqual(
34933493
util.inspect(-123456789.12345678, { numericSeparator: true }),
34943494
'-123_456_789.123_456_78'
34953495
);
3496+
3497+
// Regression test for https://github.com/nodejs/node/issues/59376
3498+
// numericSeparator should work correctly for negative fractional numbers
3499+
{
3500+
// Test the exact values from the GitHub issue
3501+
const values = [0.1234, -0.12, -0.123, -0.1234, -1.234];
3502+
assert.strictEqual(
3503+
util.inspect(values, { numericSeparator: true }),
3504+
'[ 0.123_4, -0.12, -0.123, -0.123_4, -1.234 ]'
3505+
);
3506+
3507+
// Test individual negative fractional numbers between -1 and 0
3508+
assert.strictEqual(
3509+
util.inspect(-0.1234, { numericSeparator: true }),
3510+
'-0.123_4'
3511+
);
3512+
assert.strictEqual(
3513+
util.inspect(-0.12345, { numericSeparator: true }),
3514+
'-0.123_45'
3515+
);
3516+
}
34963517
}
34973518

34983519
// Regression test for https://github.com/nodejs/node/issues/41244

0 commit comments

Comments
 (0)