Math.round() wrong calculation in as3?

不问归期 提交于 2019-12-22 00:30:17

问题


Can anyone explain this?

what am I doing wrong?


回答1:


Math.Round(x:Number) rounds x to the nearest integer value. In your case 28 is the nearest integer value for 28.499999999999996. So here the behavior is correct. What is weird is that 0.285 * 100 is not 28.5, but that is a consequence of the precision of the Number class in as3. Here is a little more information about this and a possible solution:

Innacurate math results

Also you can see this SO question:

Very strange number operation issue

Hope this helps.




回答2:


Round is doing the correct thing. 0.285 cannot be exactly represented as a binary floating point value. As you see, when multiplied by 100 it approximates to 28.4999999... which is less than 28.5, so the value is rounded down.



来源:https://stackoverflow.com/questions/9123187/math-round-wrong-calculation-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!