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

Rounding error with currency #74

Closed
ikudosi opened this issue Aug 20, 2019 · 6 comments
Closed

Rounding error with currency #74

ikudosi opened this issue Aug 20, 2019 · 6 comments
Labels

Comments

@ikudosi
Copy link

ikudosi commented Aug 20, 2019

Not sure if this is a bug or I'm missing something, however I have two records in my system with same 3rd decimal but one is not rounding up. They are both using currency("$") (which should round to 2 decimal by default).

  1. 4514.275 -> 4,514.27
  2. 9446.975 -> 9,446.98
@freearhey
Copy link
Owner

It's not really a mistake, although I agree it looks really strange. The fact is that under the hood filter currency uses the standard javascript method toFixed() which in some cases may return an unexpected result. Here is an example from docs:

2.35.toFixed(1);        // Returns '2.4'
2.55.toFixed(1);        // Returns '2.5'

Here you can read more about it:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed#Using_toFixed

@ikudosi
Copy link
Author

ikudosi commented Aug 20, 2019

Hmm thanks for the clear up. I did a quick google search regarding this issue and found a simple solution of using some logic of below:

function toFixed( num, precision ) {
    return (+(Math.round(+(num + 'e' + precision)) + 'e' + -precision)).toFixed(precision);
}

Is it possible to implement something to the currency filter? I Just found it odd that it rounds up conditionally. So far it's been causing a headache b/c our sales orders are sometimes off by .01 visually between our ERP that rounds up.

@freearhey
Copy link
Owner

Well, I will check this solution and if everything works fine I will update filter as soon as possible.

@ikudosi
Copy link
Author

ikudosi commented Aug 20, 2019

@freearhey Thanks!

freearhey added a commit that referenced this issue Aug 23, 2019
@freearhey
Copy link
Owner

Done.

@ikudosi
Copy link
Author

ikudosi commented Aug 30, 2019

@freearhey Everything looks good! Thanks again!

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

No branches or pull requests

2 participants