Share via


Mod Operator (Visual Basic) 

Divides two numbers and returns only the remainder.

number1 Mod number2

Parts

  • number1
    Required. Any numeric expression.
  • number2
    Required. Any numeric expression.

Supported Types

All numeric types, including the unsigned and floating-point types and Decimal.

Result

The result is the remainder left after number2 is divided into number1.

The / Operator (Visual Basic) returns the full quotient, which retains the remainder.

Remarks

If number1 or number2 is a floating-point value, then division is carried out and the floating-point remainder is returned. The data type of the result is the smallest data type that can hold all possible values resulting from division with the data types of number1 and number2.

If number1 or number2 evaluates to Nothing, it is treated as zero.

Attempted Division by Zero

If number2 evaluates to zero, the behavior of the Mod operator depends on the data type of the operands. An integral division throws a DivideByZeroException exception. A floating-point division returns NaN.

Equivalent Formula

In terms of traditional operators, a Mod b is equivalent to either of the following formulas:

a - (b * (a \ b))

a - (b * Int(a / b) + CSByte(Math.Sign(a) <> Math.Sign(b)))

Floating-point Imprecision

When you work with floating-point numbers, keep in mind that they do not always have a precise representation in memory. This could lead to unexpected results from certain operations, such as value comparison and the Mod operator. For more information, see Troubleshooting Data Types.

Overloading

The Mod operator can be overloaded, which means that a class or structure can redefine its behavior when an operand has the type of that class or structure. If your code uses this operator on such a class or structure, be sure you understand its redefined behavior. For more information, see Operator Procedures.

Example

The following example uses the Mod operator to divide two numbers and return only the remainder. If either number is a floating-point number, the result is a floating-point number representing the remainder.

Dim testResult As Double
testResult = 10 Mod 5
testResult = 10 Mod 3
testResult = 12 Mod 4.3
testResult = 12.6 Mod 5
testResult = 47.9 Mod 9.35

The expressions in the preceding example return values of 0, 1, 3.4, 2.6, and 1.15.

The following example demonstrates the potential imprecision of floating-point operands. In the first statement, the operands are Double, and 0.2 is an infinitely repeating binary fraction with a stored value of 0.20000000000000001. In the second statement, the literal type character D forces both operands to Decimal, and 0.2 has a precise representation.

firstResult = 2.0 Mod 0.2
' Double operation returns 0.2, not 0.
secondResult = 2D Mod 0.2D
' Decimal operation returns 0.

See Also

Tasks

Troubleshooting Data Types

Reference

Arithmetic Operators (Visual Basic)
Operator Precedence in Visual Basic
Operators Listed by Functionality

Concepts

Arithmetic Operators in Visual Basic