为什么trace(123.456 - 123)在flash as2中给我0.456000000000003
跟踪(123.456 - 123) //输出是0.456000000000003!!!!!! 为什么它给了我这个奇怪的值?我需要它输出 0.456 (我的目标是得到分数)
trace(123.456 - 123) //the output is 0.456000000000003!!!!!!
Why it gives me this strange value? i need it to output 0.456
(my aim is to have the fraction)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就像 1/3 和 1/7 不能用十进制很好地表示一样(分别为 0.33333... 和 0.142857142857...),某些十进制数也不能用二进制很好地表示,从而导致这样的错误。要解决它,请尝试以下操作:
Just like 1/3rd and 1/7th cannot be expressed well in decimal notation (0.33333... and 0.142857142857... respectively), certain decimal numbers can't be represented well in binary, leading to errors like this. To solve it, try this:
因为它是一个浮点数,你无法准确地用浮点数表示所有数字。
如果你需要一个分数,就创建一个派系类(也许AS中已经内置了一个我不知道。)
Because it's a floating point number and you can't accurately represent all numbers in floating point.
If you need a fraction, make a faction class (maybe one is built into AS already I don't know.)
因为你正在处理一个浮点数。您需要将其四舍五入:
http://board.flashkit。 com/board/showthread.php?t=778701
从上面的链接中发挥作用(数字已更改):
Because you're dealing with a floating point number. you're going to need to round it up:
http://board.flashkit.com/board/showthread.php?t=778701
exert form the above link (with numbers changed):