Udostępnij za pośrednictwem


Modulus Operator (%)

Divides the value of one expression by the value of another, and returns the remainder.

number1 % number2

Arguments

  • number1
    Any numeric expression.

  • number2
    Any numeric expression.

Remarks

The modulus, or remainder, operator divides number1 by number2 and returns only the remainder. The sign of the result is the same as the sign of number1. The value of the result is between 0 and the absolute value of number2.

The arguments to the modulus operator may be floating-point numbers, so that 5.6 % 0.5 returns 0.1.

Example

The following example illustrates a use of the modulus operator.

var myMoney : int = 128;
var cookiePrice : int = 33;
// Calculate the change if the maximum number of cookies are bought.
var change : int = myMoney % cookiePrice;
// Calculate number of cookies bought.
var numCookies : int = Math.round((myMoney-change)/cookiePrice);

Requirements

Version 1

See Also

Reference

Modulus Assignment Operator (%=)

Concepts

Operator Precedence

Operator Summary