User Guide

164 ActionScript language elements
% modulo operator
expression1 % expression2
Calculates the remainder of expression1 divided by expression2. If either of the
expression parameters are non-numeric, the modulo (%) operator attempts to convert them
to numbers. The
expression can be a number or string that converts to a numeric value.
The sign of the result of modulo operation matches the sign of the dividend (the first
number). For example,
-4 % 3 and -4 % -3 both evaluate to -1.
Availability: ActionScript 1.0; Flash Lite 1.0 - In Flash 4 files, the
% operator is expanded in
the SWF file as
x - int(x/y) * y and may not be as fast or as accurate in later versions of
Flash Player.
Operands
expression1 : Number - A number or expression that evaluates to a number.
expression2 : Number - A number or expression that evaluates to a number.
Returns
Number - The result of the arithmetic operation.
Example
The following numeric example uses the modulo (%) operator:
trace(12%5); // traces 2
trace(4.3%2.1); // traces 0.0999999999999996
trace(4%4); // traces 0
The first trace returns 2, rather than 12/5 or 2.4, because the modulo (% ) operator returns
only the remainder. The second trace returns 0.0999999999999996 instead of the expected
0.1 because of the limitations of floating-point accuracy in binary computing.
See also
/ division operator, round (Math.round method)
%= modulo assignment operator
expression1 %= expression2
Assigns expression1 the value of expression1 % expression2. The following two
statements are equivalent:
x %= y; and x = x % y;