Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2206,11 +2206,14 @@ function formatNumber(fn, number, numericSeparator) {
}
return fn(addNumericSeparator(numberString), 'number');
}
if (NumberIsNaN(number)) {
if (NumberIsNaN(number) || StringPrototypeIncludes(numberString, 'e')) {
return fn(numberString, 'number');
}

const decimalIndex = StringPrototypeIndexOf(numberString, '.');
if (decimalIndex === -1) {
return fn(addNumericSeparator(numberString), 'number');
}
const integerPart = StringPrototypeSlice(numberString, 0, decimalIndex);
const fractionalPart = StringPrototypeSlice(numberString, decimalIndex + 1);

Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-util-inspect-scientific.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

require('../common');
const assert = require('assert');
const util = require('util');

// Ensure scientific notation is not broken when numericSeparator is enabled
{
const value = 1e-7;
const result = util.inspect(value, { numericSeparator: true });

assert.strictEqual(result, '1e-7');
}

// Another scientific notation case (positive exponent)
{
const value = 1e+21;
const result = util.inspect(value, { numericSeparator: true });

assert.strictEqual(result, '1e+21');
}

// Control case: regular number should still apply numeric separators
{
const value = 1000000;
const result = util.inspect(value, { numericSeparator: true });

assert.strictEqual(result, '1_000_000');
}
Loading