Skip to content

Commit 393ff72

Browse files
Han5991marco-ippolito
authored andcommitted
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 76001f9 commit 393ff72

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
@@ -1639,23 +1639,28 @@ function formatNumber(fn, number, numericSeparator) {
16391639
}
16401640
return fn(`${number}`, 'number');
16411641
}
1642+
1643+
const numberString = String(number);
16421644
const integer = MathTrunc(number);
1643-
const string = String(integer);
1645+
16441646
if (integer === number) {
1645-
if (!NumberIsFinite(number) || StringPrototypeIncludes(string, 'e')) {
1646-
return fn(string, 'number');
1647+
if (!NumberIsFinite(number) || StringPrototypeIncludes(numberString, 'e')) {
1648+
return fn(numberString, 'number');
16471649
}
1648-
return fn(`${addNumericSeparator(string)}`, 'number');
1650+
return fn(addNumericSeparator(numberString), 'number');
16491651
}
16501652
if (NumberIsNaN(number)) {
1651-
return fn(string, 'number');
1653+
return fn(numberString, 'number');
16521654
}
1655+
1656+
const decimalIndex = StringPrototypeIndexOf(numberString, '.');
1657+
const integerPart = StringPrototypeSlice(numberString, 0, decimalIndex);
1658+
const fractionalPart = StringPrototypeSlice(numberString, decimalIndex + 1);
1659+
16531660
return fn(`${
1654-
addNumericSeparator(string)
1661+
addNumericSeparator(integerPart)
16551662
}.${
1656-
addNumericSeparatorEnd(
1657-
StringPrototypeSlice(String(number), string.length + 1),
1658-
)
1663+
addNumericSeparatorEnd(fractionalPart)
16591664
}`, 'number');
16601665
}
16611666

test/parallel/test-util-inspect.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3313,6 +3313,27 @@ assert.strictEqual(
33133313
util.inspect(-123456789.12345678, { numericSeparator: true }),
33143314
'-123_456_789.123_456_78'
33153315
);
3316+
3317+
// Regression test for https://github.com/nodejs/node/issues/59376
3318+
// numericSeparator should work correctly for negative fractional numbers
3319+
{
3320+
// Test the exact values from the GitHub issue
3321+
const values = [0.1234, -0.12, -0.123, -0.1234, -1.234];
3322+
assert.strictEqual(
3323+
util.inspect(values, { numericSeparator: true }),
3324+
'[ 0.123_4, -0.12, -0.123, -0.123_4, -1.234 ]'
3325+
);
3326+
3327+
// Test individual negative fractional numbers between -1 and 0
3328+
assert.strictEqual(
3329+
util.inspect(-0.1234, { numericSeparator: true }),
3330+
'-0.123_4'
3331+
);
3332+
assert.strictEqual(
3333+
util.inspect(-0.12345, { numericSeparator: true }),
3334+
'-0.123_45'
3335+
);
3336+
}
33163337
}
33173338

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

0 commit comments

Comments
 (0)