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(money-input): avoid rounding errors #229

Merged
merged 1 commit into from
Nov 12, 2018

Conversation

dferber90
Copy link
Contributor

When users selected the currency EUR (or any other with 2 fraction digits) and typed certain numbers like 2.49, the MoneyInput.convertToMoneyValue would determine an incorrect centAmount due to rounding errors as @adnasa hinted at in #221 (comment).

screenshot showing rounding error

This PR fixes that by ensuring centAmount is an integer.

// to the nearest decimal value
// ref: https://github.com/commercetools/merchant-center-frontend/pull/770
const centAmount = Math.trunc(
Math.round(amountAsNumber * 10 ** currency.fractionDigits)
Copy link
Contributor

Choose a reason for hiding this comment

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

🔥

@dferber90 dferber90 force-pushed the df-fix-money-value-rounding branch from ea778a6 to 3065656 Compare November 12, 2018 13:29
});

// This test ensures that rounding is used instead of just cutting the
// number of. Cutting it of would result in an incorrect 239998.
Copy link
Contributor

Choose a reason for hiding this comment

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

nice comment!

@adnasa
Copy link
Contributor

adnasa commented Nov 12, 2018

<3

@dferber90 dferber90 merged commit b92ede4 into master Nov 12, 2018
@dferber90 dferber90 deleted the df-fix-money-value-rounding branch November 12, 2018 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💅 Type: Enhancement Improves existing code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants