Precision subtracts javascript [duplicated]

0

I'm doing accounting software and I have serious problems with calculations of decimals.

Here is an example: if I do the subtraction 3.00 - 2.70 it gives me 0.29 and not 0.30 as it should

console.log(3.00-2.70);
    
asked by marco811 18.04.2018 в 11:07
source

2 answers

0

At first I thought that with the function Math.round() you would solve the problem, but when I tried it, I realized that it does not hold decimals. Therefore, when I continued to google, I found this solution, which seemed to be effective: we would have to test it with many values to see if it always returns the desired value.

console.log(parseFloat(3.00-2.70).toFixed(2));

I found the solution suggestion in Round to at most 2 decimal places (only if necessary) There you can read all the analysis that users do about possible failures, etc.

    
answered by 18.04.2018 / 13:45
source
0

Thanks for the reply. I found a library that has helped me correct this problem decimal.js

    
answered by 18.04.2018 в 16:08