Skip to content

Commit e476e43

Browse files
Han5991targos
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 f667215 commit e476e43

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
@@ -1851,23 +1851,28 @@ function formatNumber(fn, number, numericSeparator) {
18511851
}
18521852
return fn(`${number}`, 'number');
18531853
}
1854+
1855+
const numberString = String(number);
18541856
const integer = MathTrunc(number);
1855-
const string = String(integer);
1857+
18561858
if (integer === number) {
1857-
if (!NumberIsFinite(number) || StringPrototypeIncludes(string, 'e')) {
1858-
return fn(string, 'number');
1859+
if (!NumberIsFinite(number) || StringPrototypeIncludes(numberString, 'e')) {
1860+
return fn(numberString, 'number');
18591861
}
1860-
return fn(`${addNumericSeparator(string)}`, 'number');
1862+
return fn(addNumericSeparator(numberString), 'number');
18611863
}
18621864
if (NumberIsNaN(number)) {
1863-
return fn(string, 'number');
1865+
return fn(numberString, 'number');
18641866
}
1867+
1868+
const decimalIndex = StringPrototypeIndexOf(numberString, '.');
1869+
const integerPart = StringPrototypeSlice(numberString, 0, decimalIndex);
1870+
const fractionalPart = StringPrototypeSlice(numberString, decimalIndex + 1);
1871+
18651872
return fn(`${
1866-
addNumericSeparator(string)
1873+
addNumericSeparator(integerPart)
18671874
}.${
1868-
addNumericSeparatorEnd(
1869-
StringPrototypeSlice(String(number), string.length + 1),
1870-
)
1875+
addNumericSeparatorEnd(fractionalPart)
18711876
}`, 'number');
18721877
}
18731878

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)