Float sum with javascript

Possible Duplicate:
Is JavaScript's Math broken?

I'm calculating the sum of several float values using javascript and... I've noticed a strange thing never seen before. Executing this code:

parseFloat('2.3') + parseFloat('2.4')

I obtain 4.699999999999999

So... what sould I do to obtain a correct value? (supposed that this is incorrect...)


一旦你阅读了每个计算机科学家应该知道的关于浮点算术的知识,你可以使用.toFixed()函数:

var result = parseFloat('2.3') + parseFloat('2.4');
alert(result.toFixed(2));​

(parseFloat('2.3') + parseFloat('2.4')).toFixed(1);

它会给你我的解决方案

链接地址: http://www.djcxy.com/p/27442.html

上一篇: 浮点算法不能产生精确的结果

下一篇: 浮动与javascript的总和