Skip to content

Commit 7e85198

Browse files
watildeaduh95
authored andcommitted
util: fix scientific notation formatting
Signed-off-by: Daijiro Wachi <[email protected]> PR-URL: #63823 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Jordan Harband <[email protected]>
1 parent b8d3f5e commit 7e85198

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

lib/internal/util/inspect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,7 @@ function formatNumber(fn, number, numericSeparator) {
19351935
}
19361936
return fn(addNumericSeparator(numberString), 'number');
19371937
}
1938-
if (NumberIsNaN(number)) {
1938+
if (NumberIsNaN(number) || StringPrototypeIncludes(numberString, 'e')) {
19391939
return fn(numberString, 'number');
19401940
}
19411941

test/parallel/test-util-inspect.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3848,6 +3848,12 @@ assert.strictEqual(
38483848
'-0.123_45'
38493849
);
38503850
}
3851+
3852+
// Numbers in scientific notation should not get malformed separators
3853+
assert.strictEqual(util.inspect(1e-7, { numericSeparator: true }), '1e-7');
3854+
assert.strictEqual(util.inspect(1.5e-10, { numericSeparator: true }), '1.5e-10');
3855+
assert.strictEqual(util.inspect(1.23e-100, { numericSeparator: true }), '1.23e-100');
3856+
assert.strictEqual(util.inspect(1.23456789e-12, { numericSeparator: true }), '1.23456789e-12');
38513857
}
38523858

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

0 commit comments

Comments
 (0)