I faced the Floating-Point-Precision problem in JavaScript and to solve it i found libraries like Decimal.js or BigNumber.js. It was working fine for me until i tried the following.
I have a total of 60 and a share of 10. So the percentage of the share is 10/60*100 (16.666666666666666667%).
Now i want to know what 16.666666666666666667% of 360 are which is 60 (calculator) but for some reason i get 60.000000000000000001 with Decimal.js.
var percentage = (Decimal(10).div(60)).mul(100);
console.log(percentage.toString()); // 16.666666666666666667
var share = (Decimal(360).mul(percentage)).div(100);
console.log(share.toString()); // 60.000000000000000001
I expected to get 60 but got this. Am i missing something or it is a problem of the library because im a bit confused that this is not working with a library that was created for such problems.