View comments | RSS feed

round (Math.round method)

public static round(x:Number) : Number

Rounds the value of the parameter x up or down to the nearest integer and returns the value. If parameter x is equidistant from its two nearest integers (that is, the number ends in .5), the value is rounded up to the next higher integer.

Availability: ActionScript 1.0; Flash Player 5 - In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports.

Parameters

x:Number - A number.

Returns

Number - A number; an integer.

Example

The following example returns a random number between two specified integers.

function randRange(min:Number, max:Number):Number {
    var randomNum:Number = Math.round(Math.random() * (max-min+1) + (min-.5));
    return randomNum;
}
for (var i = 0; i<25; i++) {
    trace(randRange(4, 11));
}

See also

ceil (Math.ceil method), floor (Math.floor method)


Flash CS3


Comments


No screen name said on Aug 15, 2007 at 7:01 AM :
"The following example returns a random number between two specified integers"

should read

The following example returns a random integer between two specified integers"
tderich said on Sep 26, 2007 at 10:57 AM :
Integer was introduced to ActionScript in version 3.0.
Since this is an AS2 example,
Number is correct. ActionScript 2.0 simply
uses Number for any type of
numeric value.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00001837.html