Rounding Down

Rounding Down

Flash CS4 ActionScript 3.0 Tutorial


How to Round a Number 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 down using using the floor method. For more information on rounding up and basic rounding see topics, Rounding Up 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.floor(rnd01);

rnd02= Math.floor(rnd02);

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

trace("Using Floor, 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 to the floor integer value, which is two. 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.