freearhey / vue2-filters

A collection of Vue.js filters

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rounding error with currency

ikudosi opened this issue · comments

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

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

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.

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

@freearhey Everything looks good! Thanks again!