GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — util: fix numericSeparator with negative fractional numbers · nodejs/node@e476e43

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/util
    • inspect.js
  • test/parallel
    • test-util-inspect.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+14-9Lines 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

Collapse file

‎test/parallel/test-util-inspect.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js
+21Lines 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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.