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

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

问题描述

我的问题很简单来形容,在软硬度:

My problem is quite simple to describe, in flex:

0.8 - 0.2 = 0.6000000000000001

前任何人都得到了这个,我敢肯定,前两个成员是0.8和0.2,并且数量级的,为什么会出现这种情况?

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);

但仅此而已,如果我跟踪我得到0.8不0.800000000000001什么的。

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

推荐答案

这是中的所有的,因为的浮点数precision 。没有太多可以做的,因为它是一个错误值,这是可能的所有机器上。

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.

您可以做的是设置您想要从数precision:

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

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

很多时候,闪光灯圆满结束在数转化为一个字符串,这些数字对你(视觉显示),除去大部分的这些错误被显示给用户。

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天全站免登陆