parseFloat的精度问题
<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>
a,b,c相加本来为1.3921,但sum1得出的结果为:1.3921000000000001,显然不正确,通过toFixed(n)方法修正后,得到正确结果。
引用:http://bianqioujin.iteye.com/blog/347199