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

Wrong number of digits after the decimal point #3

Closed
jansivans opened this issue Jan 11, 2021 · 2 comments
Closed

Wrong number of digits after the decimal point #3

jansivans opened this issue Jan 11, 2021 · 2 comments

Comments

@jansivans
Copy link

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.

@super-ienien
Copy link
Owner

super-ienien commented Jan 11, 2021

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)));

@super-ienien
Copy link
Owner

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

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

No branches or pull requests

2 participants