Skip to content

Commit f660943

Browse files
Han5991richardlau
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 <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 658c31d commit f660943

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
@@ -3484,6 +3484,27 @@ assert.strictEqual(
34843484
util.inspect(-123456789.12345678, { numericSeparator: true }),
34853485
'-123_456_789.123_456_78'
34863486
);
3487+
3488+
// Regression test for https://github.com/nodejs/node/issues/59376
3489+
// numericSeparator should work correctly for negative fractional numbers
3490+
{
3491+
// Test the exact values from the GitHub issue
3492+
const values = [0.1234, -0.12, -0.123, -0.1234, -1.234];
3493+
assert.strictEqual(
3494+
util.inspect(values, { numericSeparator: true }),
3495+
'[ 0.123_4, -0.12, -0.123, -0.123_4, -1.234 ]'
3496+
);
3497+
3498+
// Test individual negative fractional numbers between -1 and 0
3499+
assert.strictEqual(
3500+
util.inspect(-0.1234, { numericSeparator: true }),
3501+
'-0.123_4'
3502+
);
3503+
assert.strictEqual(
3504+
util.inspect(-0.12345, { numericSeparator: true }),
3505+
'-0.123_45'
3506+
);
3507+
}
34873508
}
34883509

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

0 commit comments

Comments
 (0)