-
-
Notifications
You must be signed in to change notification settings - Fork 11
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 bug with negative numbers #8
Conversation
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.
Looks good, handing over to @sindresorhus to approve
@sindresorhus Should we remove support for node < 4 I guess? |
Same answer as sindresorhus/to-single-quotes#14 (comment). |
@ruipneves Can you give the pull request a more descriptive title. Also, always link to the issue you're fixing using |
@@ -30,7 +30,7 @@ | |||
"increment" | |||
], | |||
"dependencies": { | |||
"number-is-integer": "^1.0.0" | |||
"number-is-integer": "^1.0.1" |
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.
Don't do unrelated changes. This change also makes no difference.
@@ -14,6 +14,9 @@ function round(fn, x, precision) { | |||
var exponentNeg = precision > 0 ? 'e-' : 'e'; | |||
precision = Math.abs(precision); | |||
|
|||
if (fn === 'round') { | |||
return Number(Math.sign(x) * (Math[fn](Math.abs(x) + exponent + precision) + exponentNeg + precision)); |
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.
Math[fn]
=> Math.round
. We already know what the fn
is.
@sindresorhus Thank you again for your feedback! The requested changes have been made. |
134 was just an example. That should be the actual issue number of what you're fixing. I'd recommend reading https://guides.github.com/features/mastering-markdown/ and https://github.com/tiimgreen/github-cheat-sheet |
Thank you @ruipneves :) |
I was looking for Math.round() documentation when I came across this bug. After concluding that roundTo was not behaving as expected I tried to fix the problem. The problem was that with negative numbers, Math.round() would go to the hight number, thus, not producing the expected results.
To solve this I checked if the requested function was the "round" function and if this was verified then the function would be applied to the absolute value of the number to round and then multiplied by it's original sign.
Is this the correct way to do it?
Any feedback is appreciated.
Fixes #6