super-ienien/percent-round

Wrong number of digits after the decimal point

jansivans opened this issue · 2 comments

Hi.
With such input:

percentRound([1, 1, 1], 2);

Result is:

[
    33.340000000001936,
    33.33000000000194,
    33.33000000000194
]

I was expecting to have just 2 numbers after the decimal point here.

Hi,

Thanks for reporting this. It is due to how javascript handles floating points : some explanation here

in javascript : var x = 0.2 + 0.1; // x will be 0.30000000000000004

I will look into a fix for this. In the mean time as a workaround you can do this :

percentRound([1, 1, 1], 2).map(result => parseFloat(result.toFixed(2)));

I fixed this by treating the decimal numbers as integers and convert them back to floats after all the computation are done