Export (0) Print
Expand All
10 out of 19 rated this helpful - Rate this topic

Modulus Operator (%) 

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

number1 % number2

Any numeric expression.


Any numeric expression.

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.

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);
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft. All rights reserved.