Flash Lite 2.x and 3.0 ActionScript Language Reference

% 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.

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)