-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
Conversation
Instead of fixing case 100: // 100 ('d') and 105 are ('i') should be handled in the exact same way
case 105: {
// ...
} This change also requires an update to |
Marking this as semver-major, since it changes the output of console.log & util.format. |
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'); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) :)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
@nodejs/util |
9594d66
to
6552e03
Compare
This seems like it's both a breaking change, and also one that makes node strictly less useful solely to conform with browsers. Why is this desirable for a core module? |
@ljharb It is the expected behavior of JavaScript. |
@OguzhanUmutlu how so? "the web" isn't the same as "javascript", and while "%i" definitely tells me "integer" (in many languages), "%d" tells me "decimal", which is decidedly not integers (across many languages as well). |
@ljharb while I see where you are coming from, I would like to note that the official description of utils.format in the NodeJS docs is:
%d is standard for a In addition, I'm not sure how this deviation of %d is useful - if you need floating point precision, you'd want the first position after the decimal even if it was zero, in which case you would use However, this is not my call to make, so I'm more than happy to revise based on the feedback from the maintainers. |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit torn what to do here. The suggestion is definitely valid, as it aligns with the "normal" %d behavior. In Node.js it has just always worked as "decimal", and some code might rely upon the current behavior - especially debug logs. I can't think of a situation where changing the behavior now would improve a users experience and therefore, I wonder if it makes sense to keep the current behavior as legacy oversight.
This would need a rebase and a fix for the the linter failure. |
This issue/PR was marked as stalled, it will be automatically closed in 30 days. If it should remain open, please leave a comment explaining why it should remain open. |
Hey, I've marked this PR as |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about it more and this could definitely cause difficult to detect problems for users to change this. Imagine sensor data is formatted using util.format()
and now the decimal part would just be ignored. It is pretty much impossible to detect the changed behavior (integers would still be legit) but it would break the users expected behavior.
For new users, it's easy to switch to %i for integers, as they will immediately see that %d is not used like that (as it is documented).
Overall, I don't think it's worth the risk of breaking applications/causing data loss for that.
Fixes #48238
According to the docs at https://console.spec.whatwg.org/#formatter, both %d and %i should parse to an integer.
Matched the parsing method of %d to the same as %i, and corrected tests with improper assertions given this information.