Rounding Up

Rounding Up

Flash CS4 ActionScript 3.0 Tutorial

 

How to Round a Number Up 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 basic rounding see topics, Rounding Down and Basic Rounding.

 

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.ceil(rnd01);

rnd02= Math.ceil(rnd02);

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

trace("Using Ceiling, 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 up to the next integer value, which is three. The following line does the same thing for the second variable.

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