Calculations use too many decimals

Hi, I have a problem while trying with my calculations on the last project. I use numbers that are up to 2 decimals (ex 3.26) and then after i print something it looks like this 36.739999999999995 . That is a problem because it never reaches zero and another number that i might need . I have tried parseFloat(num).toFixed(2) but it rounds the number wrong (doesn’t take into account the 3rd decimal) and a function

function roundToTwo(num) {
return +(Math.round(num + “e+2”) + “e-2”);
}

It still doesn’t work.
If anyone has come across this problem any help would be much appreciated. Thank you in advance

Floating point numbers always have rounding errors. I would use an integer number of cents.

But for the problem to work correctly i need to do calculations up to 2 decimal for example 0.04 .I cant do that with an integer

Right, so you use

You convert to cents instead of dollars.

1 Like

Thank you for your advice ! I solved it.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.