Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: both %i and %d should parse floats to int according to Formatter docs #48246

Closed
Closed
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
1 change: 1 addition & 0 deletions benchmark/util/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const inputs = {
'string': ['Hello, my name is %s', 'Fred'],
'string-2': ['Hello, %s is my name', 'Fred'],
'number': ['Hi, I was born in %d', 1989],
'number-2': ['Hi, I am about %d years old', 41.32],
'replace-object': ['An error occurred %j', { msg: 'This is an error' }],
'unknown': ['hello %a', 'test'],
'no-replace': [1, 2],
Expand Down
4 changes: 1 addition & 3 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,7 @@ corresponding argument. Supported specifiers are:
and `-0`. `BigInt` values will be represented with an `n` and Objects that
have no user defined `toString` function are inspected using `util.inspect()`
with options `{ depth: 0, colors: false, compact: 3 }`.
* `%d`: `Number` will be used to convert all values except `BigInt` and
`Symbol`.
* `%i`: `parseInt(value, 10)` is used for all values except `BigInt` and
* `%d` and `%i`: `parseInt(value, 10)` is used for all values except `BigInt` and
`Symbol`.
* `%f`: `parseFloat(value)` is used for all values expect `Symbol`.
* `%j`: JSON. Replaced with the string `'[Circular]'` if the argument contains
Expand Down
13 changes: 1 addition & 12 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const {
MathRound,
MathSqrt,
MathTrunc,
Number,
NumberIsFinite,
NumberIsNaN,
NumberParseFloat,
Expand Down Expand Up @@ -2216,17 +2215,6 @@ function formatWithOptionsInternal(inspectOptions, args) {
case 106: // 'j'
tempStr = tryStringify(args[++a]);
break;
case 100: { // 'd'
const tempNum = args[++a];
if (typeof tempNum === 'bigint') {
tempStr = formatBigIntNoColor(tempNum, inspectOptions);
} else if (typeof tempNum === 'symbol') {
tempStr = 'NaN';
} else {
tempStr = formatNumberNoColor(Number(tempNum), inspectOptions);
}
break;
}
case 79: // 'O'
tempStr = inspect(args[++a], inspectOptions);
break;
Expand All @@ -2238,6 +2226,7 @@ function formatWithOptionsInternal(inspectOptions, args) {
depth: 4,
});
break;
case 100: // 'd'
case 105: { // 'i'
const tempInteger = args[++a];
if (typeof tempInteger === 'bigint') {
Expand Down
19 changes: 10 additions & 9 deletions test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ assert.strictEqual(util.format('%d', 42.0), '42');
assert.strictEqual(util.format('%d', 42), '42');
assert.strictEqual(util.format('%d', '42'), '42');
assert.strictEqual(util.format('%d', '42.0'), '42');
assert.strictEqual(util.format('%d', 1.5), '1.5');
assert.strictEqual(util.format('%d', -0.5), '-0.5');
assert.strictEqual(util.format('%d', -0.0), '-0');
assert.strictEqual(util.format('%d', ''), '0');
assert.strictEqual(util.format('%d', 1.5), '1');
assert.strictEqual(util.format('%d', -0.5), '-0');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: in chrome console.log('%d', '-0.5') outputs 0

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I'm not familiar with that acronym 😅 though this is a good point of difference between both integer formats in server and in browser (confirmed in Firefox as well).

Should this be corrected (likely not in this unit of work) or just noted?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT = Nitpick (something that doesn't affect the overall PR and can, but doesn't necessarily have to be updated in order for it to be mergeable) :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that node and chrome are v8, that seems more important to align with than firefox here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed 100%, was just noting that Firefox behaved the same as Chrome in this situation with the sign flip, so it's not specific to just Chrome/V8

assert.strictEqual(util.format('%d', -0.0), '0');
assert.strictEqual(util.format('%d', ''), 'NaN');
assert.strictEqual(util.format('%d', ' -0.000'), '-0');
assert.strictEqual(util.format('%d', Symbol()), 'NaN');
assert.strictEqual(util.format('%d', Infinity), 'Infinity');
assert.strictEqual(util.format('%d', -Infinity), '-Infinity');
assert.strictEqual(util.format('%d', Infinity), 'NaN');
assert.strictEqual(util.format('%d', -Infinity), 'NaN');
assert.strictEqual(util.format('%d %d', 42, 43), '42 43');
assert.strictEqual(util.format('%d %d', 42), '42 %d');
assert.strictEqual(
util.format('%d', 1180591620717411303424),
'1.1805916207174113e+21'
'1'
);
assert.strictEqual(
util.format('%d', 1180591620717411303424n),
Expand All @@ -80,7 +80,7 @@ assert.strictEqual(

assert.strictEqual(
util.format('%d', 1180591620717411303424),
'1.1805916207174113e+21'
'1'
);

assert.strictEqual(
Expand Down Expand Up @@ -114,6 +114,7 @@ assert.strictEqual(util.format('%i', '42'), '42');
assert.strictEqual(util.format('%i', '42.0'), '42');
assert.strictEqual(util.format('%i', 1.5), '1');
assert.strictEqual(util.format('%i', -0.5), '-0');
assert.strictEqual(util.format('%i', -0.0), '0');
assert.strictEqual(util.format('%i', ''), 'NaN');
assert.strictEqual(util.format('%i', Infinity), 'NaN');
assert.strictEqual(util.format('%i', -Infinity), 'NaN');
Expand Down Expand Up @@ -416,7 +417,7 @@ assert.strictEqual(util.format('o: %O, a: %O'), 'o: %O, a: %O');

// Invalid format specifiers
assert.strictEqual(util.format('a% b', 'x'), 'a% b x');
assert.strictEqual(util.format('percent: %d%, fraction: %d', 10, 0.1),
assert.strictEqual(util.format('percent: %d%, fraction: %f', 10, 0.1),
'percent: 10%, fraction: 0.1');
assert.strictEqual(util.format('abc%', 1), 'abc% 1');

Expand Down