ASCB阅读笔记四、Numbers and Math

阅读更多
显示最近的整数(四舍五入)
Math.round(204.499)  // 204
Math.round(401.5)    // 402

指定向上或向下
Math.floor(204.99)   // 204
Math.ceil(401.01)    // 402

控制精确度
Math.round(90.337 / .01) * .01  // 90.34
Math.round(92.5 / 5) * 5        // 95
Math.round(92.5 / 10) * 10      // 90

由此可以写一个Util类
NumberUtil.round(Math.PI)         // 3
NumberUtil.round(Math.PI, .01)    // 3.14
NumberUtil.round(Math.PI, .0001)  // 3.1416
NumberUtil.round(123.456, 1)      // 123
NumberUtil.round(123.456, 6)      // 126
NumberUtil.round(123.456, .01)    // 123.46

NumberFormat也是ASCB的一个Util类
var styler:NumberFormat = new NumberFormat("#,###,###,###.00");
Locale.slanguage = "fr";
trace(styler.format(1234));                                         // 1.234,00
trace(styler.format(12345, {group: ",", decimal: "."}));            // 12,345.00
trace(styler.format(123456));                                       // 123.345,00
Locale.slanguage = "en";
trace(styler.format(1234567));                                      // 1,234,567.00
trace(styler.format(12345678, new Locale("es", "ES")));             // 12.345.678,00
trace(styler.format(123456789, {group: "|", decimal: ","}));        // 123|456|789,00

Currency Amounts Formatting
var styler:NumberFormat = new NumberFormat();
trace(styler.currencyFormat(123456));                                  // $123,456.00
Locale.slanguage = "nl";
trace(styler.currencyFormat(123456));                                  // 123.456,00
trace(styler.currencyFormat(123456, new Locale("sv")));                // 123,456.00kr
trace(styler.currencyFormat(123456, {group: ",", decimal: ".",
                                     currency: "@", before: false}));  // 123,456.00@

生成随机数
// Generate a random integer from 0 to 100.
trace(NumberUtilities.random(0, 100));

// Generate a random multiple of 5 from 0 to 100.
trace(NumberUtilities.random(0, 100, 5));

// Generate a random number from -10 to 10, rounded to the
// nearest tenth.
trace(NumberUtilities.random(-10, 10, .1));

// Generate a random number from -1 to 1, rounded to the
// nearest five-hundredth.
trace(NumberUtilities.random(-1, 1, .05));

掷硬币
var randomNumber:Number = NumberUtilities.random(0, 1);
var text = (randomNumber == 0) ? "heads" : "tails";

掷骰子
var die1:uint = NumberUtilities.random(1, 6);
var die2:uint = NumberUtilities.random(1, 6);
var dice:uint = die1 + die2;

玩纸牌
// Deal four hands.
var hands:Array = cards.deal(4);

生成唯一数
// Display a unique number.
trace(NumberUtilities.getUnique());

角度转换
var converterToRadians:Converter = Unit.DEGREE.getConverterTo(Unit.RADIAN);
var converterToDegrees:Converter = Unit.RADIAN.getConverterTo(Unit.DEGREE);
trace(converterToRadians.convertWithLabel(1));
trace(converterToRadians.convertWithLabel(57.2957795130823));
trace(converterToDegrees.convertWithLabel(1));
trace(converterToDegrees.convertWithLabel(0.0174532925199433));

/*
   Displays:
   0.0174532925199433 radians
   1 radian
   57.2957795130823 degrees
   1 degree
*/

计算两点之间的距离
var c:Number = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));

计算圆上的点
x1 = x0 + (Math.cos(angle) * radius);
y1 = y0 + (Math.sin(angle) * radius);

量度转换
// Display the categories that are supported.
trace(Unit.getCategories(  ));

// Display the units supported in the temperature category.
trace(Unit.getUnits("temperature"));

var converter:Converter = Unit.CELCIUS.getConverterTo(Unit.FAHRENHEIT);
trace(converter.convert(0));  // Displays: 32

你可能感兴趣的:(actionscript)