% Operator (C# Reference)
Visual Studio 2008
The modulus operator (%) computes the remainder after dividing its first operand by its second. All numeric types have predefined modulus operators.
User-defined types can overload the % operator (see operator). When a binary operator is overloaded, the corresponding assignment operator, if any, is also implicitly overloaded.
Beware of rounding errors
//Try this before relying too heavily on the Modulus operator:
double a = 5.5;
double b = 1.1;
double c = a % b;
// c returns: 1.0999999999999996
// since a(5.5) is perfectly divisibly by b(1.1), shouldn't c=0? This isn't even close!
// Similar results with variables of type: float
// decimal works fine!
double a = 5.5;
double b = 1.1;
double c = a % b;
// c returns: 1.0999999999999996
// since a(5.5) is perfectly divisibly by b(1.1), shouldn't c=0? This isn't even close!
// Similar results with variables of type: float
// decimal works fine!
- 10/26/2010
- DanWoerner