Flex/AS3 很奇怪的简单数字操作问题 [英] Flex/AS3 very strange simple Number operation issue

查看:29
本文介绍了Flex/AS3 很奇怪的简单数字操作问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很容易描述,在 flex 中:

My problem is quite simple to describe, in flex:

0.8 - 0.2 = 0.6000000000000001

以前有没有人遇到过,我确定前两个成员是 0.8 和 0.2 并且是 Number 类,为什么会发生这种情况??

Anyone got this before, I'm sure the first two members are 0.8 and 0.2 and are Number class, why would this happen??

还有一件事,我将 * 类型的值转换成这样:

And another thing, I cast the value from * type like this:

var value:*=0.8;
var castValue:Number = Number(value);

但仅此而已,如果我跟踪 value,我会得到 0.8 而不是 0.800000000000001 之类的.

But nothing more, if I trace value I get 0.8 not 0.800000000000001 or something.

推荐答案

这是所有语言中相当普遍的问题,因为浮点数精度.您无能为力,因为它是所有机器上都可能出现的错误值.

This is a fairly common problem in all languages because of Floating Point Number Precision. There's not much you can do about as it is an error value that's possible on all machine.

您可以做的是从数字中设置您想要的精度:

What you can do is set a precision that you want from the number:

trace(castValue.toFixed(5)); // Gives five decimals after the point

通常,Flash 在将 Number 转换为 String(用于视觉显示)时会为您舍入这些数字,从而消除大多数这些错误对用户可见.

Often times, Flash rounds off these number for you in the conversion of Number into a String (for visual displaying) which removes most of these errors from being viewable to the user.

这篇关于Flex/AS3 很奇怪的简单数字操作问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆