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(Update toRoundedUnit to properly round negative values) #10

Merged
merged 1 commit into from
Apr 19, 2018

Conversation

tarnelope
Copy link

Math.round() doesn't correctly round negative values so this change first rounds the absolute value of the given amount and then multiplies in the Math.sign() afterwards.

This is my first contribution so happy for any feedback and change requests.

fix #7

@coveralls
Copy link

coveralls commented Apr 18, 2018

Pull Request Test Coverage Report for Build 88

  • 0 of 0 (NaN%) changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 82: 0.0%
Covered Lines: 77
Relevant Lines: 77

💛 - Coveralls

Copy link
Collaborator

@sarahdayan sarahdayan left a comment

Choose a reason for hiding this comment

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

Thanks for this! There is very little to edit in the PR itself.

There's an issue with the commit message though. I think you got mixed up with scope and commit message. Your commit message should look more like: fix(dinero#toRoundedUnit): Update toRoundedUnit to properly round negative values

There also are typos in the long form message.

I think you can manually change the commit message but if not this may need to be a new PR 😕.

src/dinero.js Outdated
const roundedUnit =
sign * Math.round(Math.abs(calculator.multiply(this.toUnit(), factor)))

return calculator.divide(roundedUnit, factor)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since sign and roundedUnit are only used once, they could be avoided altogether.

return calculator.divide(
  Math.sign(this.toUnit()) * Math.round(Math.abs(calculator.multiply(this.toUnit(), factor))),
  factor
)

@tarnelope
Copy link
Author

Sorry about that! I noticed those afterwards and wasn't sure if I could edit the message but I think I figured it out.

Copy link
Collaborator

@sarahdayan sarahdayan left a comment

Choose a reason for hiding this comment

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

One last thing and we'll be good to go!

src/dinero.js Outdated
return calculator.divide(
Math.round(calculator.multiply(this.toUnit(), factor)),
Math.sign(this.toUnit()) *
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was just thinking this should be done with Calculator#multiply as well. Will be more future-proof when support for big integers is added.

calculator.multiply(Math.sign(this.toUnit()), Math.round(Math.abs(calculator.multiply(this.toUnit(), factor))))

…ative values

Math.round() intrinsically does not correctly round negative values so this change first rounds the
absolute value of the given amount and then multiplies in the Math.sign() afterwards.

fix dinerojs#7
@sarahdayan sarahdayan merged commit 3416ff4 into dinerojs:master Apr 19, 2018
@sarahdayan
Copy link
Collaborator

🎉 This PR is included in version 1.0.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants