ActionScript3 CookBook 笔记

来源:互联网 发布:一淘网是淘宝的吗 编辑:程序博客网 时间:2024/05/01 01:38

隐藏右键菜单:
stage.showDefaultContextMenu = false;

四舍五入取近似值:
解决办法
用 Math.round( ) 进行四舍 五入 , Math.floor( ) 和 Math.ceil( ) 进行
NumberUtilities.round( ) 方法可自定义取值。
讨论
很多情况我们需要得到整数部分而不是带有小数的浮点数。比如计算出结果
望的结果应该是 4.0 。
Math.round( ) 方法进行四舍五入计算:
trace(Math.round(204.499)); // 显示 : 204
trace(Math.round(401.5)); // 显示 : 402
Math.floor( ) 方法去掉小数部分, Math.ceil( ) 方法去掉小数部分后自动加 1 :
trace(Math.floor(204.99)); // 显示 : 204
trace(Math.ceil(401.01)); // 显示 : 402
如果我想要把 90.337 四舍五入到 90.34, 可以这么写:
trace (Math.round(90.337 / .01) * .01); // 显示 : 9.34
trace (Math.round(92.5 / 5) * 5); // 显示 : 95
trace (Math.round(92.5 / 10) * 10); // 显示 : 90
更好的办法是用自定义函数 NumberUtilities.round( ) ,它需要两个参数:
number :要舍入的数字
roundToInterval :间隔值
NumberUtilities 类在 ascb.util 包中。
imported ascb.util.NumberUtilities 导入
trace(NumberUtilities.round(Math.PI)); // 显示 : 3
trace(NumberUtilities.round(Math.PI, .01)); // 显示 : 3.14
trace(NumberUtilities.round(Math.PI, .0001)); // 显示 : 3.1416
trace(NumberUtilities.round(123.456, 1)); // 显示 : 123
trace(NumberUtilities.round(123.456, 6)); // 显示 : 126
trace(NumberUtilities.round(123.456, .01)); // 显示 : 123.46

原创粉丝点击