我有:
onclick="document.getElementById('field1').value =
Math.round((parseFloat(document.getElementById('field2').value,2)*100))/100 +
Math.round((parseFloat(document.getElementById('field3').value,2)*100))/100;"
大多数数字可以舍入到两个小数点,这正是我所需要的。
然而,对于像下面这样的例子:
onclick="document.getElementById('field1').value =
Math.round((parseFloat(document.getElementById('21.29').value,2)*100))/100 +
Math.round((parseFloat(document.getElementById('54.70').value,2)*100))/100;"
第一个字段返回了75.99000000000001
,如何保持一致地将其修剪为75.99
?
Number(num.toFixed(2))
。另外,它不能变成整数,因为整数没有小数。 - Daniel Apt0.999.toFixed(2)
does not trim to0.99
, but rounds up to1
- cdalxndr