添加2十进制数给出错误的结果动作脚本脚本、错误、动作、结果

2023-09-09 21:49:21 作者:颜

我有下面的语句问题

跟踪(10.12 + 13.75) //输出23.869999999999997

trace(10.12+13.75) //output 23.869999999999997

任何人都可以解释我为什么会这样,以及如何得到确切的23.87摆脱这种?

Can anybody explain me why is this so and how to get exact 23.87 out of this?

感谢

推荐答案

这是因为的IEEE格式的precision。

That happens because of the precision of IEEE format.

最简单的办法是使用toFixed

Simplest would be to use toFixed.

var num:Number = 10.12+13.75;
var numStr:String = num.toFixed(2);
var num2:Number = new Number(numStr);