ActionScript 3 浮点数的舍入误差
示例
/**
* @param n Number to be rounded.
* @param precision Decimal places.
* @return Rounded Number
*/
function roundDecimal(n:Number, precision:Number):Number {
var factor:int = Math.pow(10, precision);
return (Math.round(n * factor) / factor);
}例子:
trace(0.9 - 1); //-0.09999999999999998 trace(roundDecimal(0.9 - 1, 1)); //-0.1 trace(roundDecimal(0.9 - 1, 2)); //-0.1 trace(roundDecimal(0.9 - 1.123, 1)); //-0.2 trace(roundDecimal(0.9 - 1.123, 2)); //-0.22 trace(roundDecimal(0.9 - 1.123, 3)); //-0.223
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语