Basic Rounding

Basic Rounding

Flash CS4 ActionScript 3.0 Tutorial

 

How to Round a Number Up or Down to a Whole Number with Flash ActionScript

 

 

With ActionScript, you can round a number up (ceiling), round a number down (floor) or round up or down depending on the fractional value.

In this example we introduce two variables that are rounded up using using the ceil() method. For more information on rounding up and rounding down see topics, Rounding Down and Rounding Up.

 

Step One

Add a new layer called Actions

Step Two

Option double-click (PC - Alt double-click) the keyframe in the Actions layer to open the ActionScript panel.

Step Three

Copy/Paste the following code to the ActionScript panel.

var rnd01:Number=2.35;

var rnd02:Number=5.75;

rnd01 = Math.round(rnd01);

rnd02= Math.round(rnd02);

trace("Using Rounding, 2.35 becomes " + rnd01);

trace("Using Rounding, 5.75 becomes " + rnd02);

Step Four

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). The new rounded values display in the Output panel.

Dissecting the Code

The first two lines of the code establish two number variables named rnd01, and rnd02 and sets the initial value for each variable. The next line rounds the first variable down since the fractional value is less than .5. The following line rounds the value up since .75 is larger than .5.

The trace statements display the new rounded values in the Output panel along with some descriptive text.

 

Stock Photos from 123RF