﻿ flex – 如何处理Actionscript中的Number精度？ - 代码日志

#### flex – 如何处理Actionscript中的Number精度？

140475.32变成140475.31999999999998

``````var toFixed:Function = function(number:Number, factor:int) {
return Math.round(number * factor)/factor;
}
``````

``````trace(toFixed(0.12345678, 10)); //0.1
``````

>乘以0.12345678乘10;那给我们1.2345678。
>当我们回合1.2345678时，我们得到1.0，
>最后，1.0除以10等于0.1。

``````trace(toFixed(1.7302394309234435, 10000)); //1.7302
``````

>乘以1.7302394309234435乘以10000;这给了我们17302.394309234435。
>当我们轮到17302.394309234435我们得到17302，
>最后，17302除以10000等于1.7302。

``````var setPrecision:Function = function(number:Number, precision:int) {
precision = Math.pow(10, precision);
return Math.round(number * precision)/precision;
}

var number:Number = 10.98813311;
trace(setPrecision(number,1)); //Result is 10.9
trace(setPrecision(number,2)); //Result is 10.98
trace(setPrecision(number,3)); //Result is 10.988 and so on
``````

http://stackoverflow.com/questions/632802/how-to-deal-with-number-precision-in-actionscript