日期:2014-05-16 浏览次数:20330 次
<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)方法修正后(n是精确的小数点位数),得到正确结果。?
例如:parseFloat(
1.392143
).toFixed(
3
)
=
1.392
;