我有這樣的代碼在javascript:爲什麼浮動變得字符串中的JavaScript
var childrenSize = 7;
var flAmt = parseFloat(100);
var amt = parseFloat(flAmt/childrenSize);
//Rounding-off fix
var newAmt = (amt.toFixed(2))*childrenSize;
alert(newAmt);
var excess = 0;
if(newAmt != flAmt)
excess = parseFloat(flAmt - newAmt);
amt = parseFloat(amt) + excess.toFixed(2);
alert(amt);
amt
應該是14.25。但相反,它變成14.285714285714286-0.03 ...爲什麼?
另外,我該如何改進我的代碼?
_「應該是100」_爲什麼?您將100除以7. – j08691 2014-10-27 14:52:48
您期待什麼結果? http://jsfiddle.net/5zr7fz0z/ – 2014-10-27 14:53:21
@ j08691它應該是應該是14.25 – newbie 2014-10-27 14:57:03