Repository navigation
util.inspect incorrectly formats negative fractional numbers with numericSeparator: true #59376
Copy link
Copy link
Closed
Labels
confirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.utilIssues and PRs related to the built-in util module.Issues and PRs related to the built-in util module.
Description
Activity
I believe the bug is caused by the following
formatNumbercode inlib/internal/util/inspect.js:const integer = MathTrunc(number); const string = String(integer); ... return fn(`${ addNumericSeparator(string) }.${ addNumericSeparatorEnd( StringPrototypeSlice(String(number), string.length + 1), ) }`, 'number'); }
For values where
-1 < number < 0, this results in:integer === -0(correct)string === '0'(loses the leading minus sign due to coercion)addNumericSeparator(string)omits the minus sign from the formatted numberStringPrototypeSlice(String(number), string.length + 1)incorrectly retains the original decimal point, resulting in a duplicated dot (..) in the output
One potential fix would be to use logic similar to the non-
numericSeparatorcase earlier in the function:const string = ObjectIs(number, -0) ? '-0' : String(integer);
However, a cleaner and more robust approach might be to either:
- Remove the sign before applying numeric separators, and prepend the minus sign to the final output if
number < 0 || ObjectIs(number, -0); or - Perform the string conversion once, then locate the decimal point (if any) and split the string appropriately for formatting.
- addedconfirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.utilIssues and PRs related to the built-in util module.Issues and PRs related to the built-in util module.
on Aug 6, 2025 I'll give it a try?
Reacted by injae kimReacted by injae kimReacted by injae kimI would either go with the quick fix suggested here or by performing the string conversion once.
@Han5991 go ahead. If you open a PR, please reference this issue with
Closes #59376.Reacted by sangwook and injae kimReacted by injae kim- added a commit that references this issue
on Aug 6, 2025 - added a commit that references this issue
on Sep 8, 2025 - added a commit that references this issue
on Sep 9, 2025 - added a commit that references this issue
on Sep 20, 2025
Metadata
Metadata
Assignees
Labels
confirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.utilIssues and PRs related to the built-in util module.Issues and PRs related to the built-in util module.
Version
v24.5.0 (also reproduced on v20.19.2)
Platform
Subsystem
util
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
Always reproducible for negative numbers strictly between
-1and0, whennumericSeparator: trueis specified.What is the expected behavior? Why is that the expected behavior?
The output should be syntactically valid JavaScript numeric literals representing the original values, with numeric separators inserted in accordance with the
numericSeparatoroption. For the sample above, the expected output is::What do you see instead?
The actual output of the sample script is:
The three incorrect values:
0..)Additional information
No response