The parseFloat() method in Javascript causes precision issues
Copy the code code as follows:
<script language="javascript">
var a = "0.11";
var b = "0.2801";
var c = "1.002";
var sum1 = parseFloat(a) + parseFloat(b) + parseFloat(c);
var sum2 = (parseFloat(a) + parseFloat(b) + parseFloat(c)).toFixed(4)
document.write("a+b+c=" + sum1);
document.write("<br/>")
document.write("a+b+c=" + sum2)
</script>
The sum of a, b, and c is originally 1.3921, but the result of sum1 is: 1.3921000000000001. This is not the desired result, especially when calculating money, such precision is not required. You can use the toFixed(n) method to correct it (n is the precise number of digits after the decimal).
For example: parseFloat(1.392143).toFixed(2)=1.39.