Skip to content

Commit a700075

Browse files
committed
util: fix numericSeparator handling for scientific notation
1 parent 4e332e0 commit a700075

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

lib/internal/util/inspect.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2206,11 +2206,14 @@ function formatNumber(fn, number, numericSeparator) {
22062206
}
22072207
return fn(addNumericSeparator(numberString), 'number');
22082208
}
2209-
if (NumberIsNaN(number)) {
2209+
if (NumberIsNaN(number) || StringPrototypeIncludes(numberString, 'e')) {
22102210
return fn(numberString, 'number');
22112211
}
22122212

22132213
const decimalIndex = StringPrototypeIndexOf(numberString, '.');
2214+
if (decimalIndex === -1) {
2215+
return fn(addNumericSeparator(numberString), 'number');
2216+
}
22142217
const integerPart = StringPrototypeSlice(numberString, 0, decimalIndex);
22152218
const fractionalPart = StringPrototypeSlice(numberString, decimalIndex + 1);
22162219

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const util = require('util');
6+
7+
// Ensure scientific notation is not broken when numericSeparator is enabled
8+
{
9+
const value = 1e-7;
10+
const result = util.inspect(value, { numericSeparator: true });
11+
12+
assert.strictEqual(result, '1e-7');
13+
}
14+
15+
// Another scientific notation case (positive exponent)
16+
{
17+
const value = 1e+21;
18+
const result = util.inspect(value, { numericSeparator: true });
19+
20+
assert.strictEqual(result, '1e+21');
21+
}
22+
23+
// Control case: regular number should still apply numeric separators
24+
{
25+
const value = 1000000;
26+
const result = util.inspect(value, { numericSeparator: true });
27+
28+
assert.strictEqual(result, '1_000_000');
29+
}

0 commit comments

Comments
 (0)