Visual Basic Language Reference  

Mod Operator

Divides two numbers and returns only the remainder.

number1 Mod number2

Parts

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

Result

The result is the remainder left after division is performed on number1 and number2.

Supported Types

Byte, Short, Integer, Long, Single, Double, Decimal

Remarks

The Mod operator divides number1 by number2 and returns only the remainder as result. For example, in the following expression, A (result) equals 2.

A = 8 Mod 3

If number1 or number2 are floating-point values, then division is carried out and the floating-point remainder is returned. The data type of the result is the same as that of the data type with the greatest range. The order of range, from least to greatest range, is Byte, Short, Integer, Long, Single, Double, and Decimal.

If any expression is stated as Nothing, or Empty, it is treated as zero. If division by zero occurs, the Mod operation returns NaN (Not a Number).

A Mod B is the equivalent of A - Int(A / B) * B + CLng(Math.Sign(A) <> Math.Sign(B)) * B

Example

This 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 myResult As Double
myResult = 10 Mod 5   ' Returns 0.
myResult = 10 Mod 3   ' Returns 1.
myResult = 12 Mod 4.3   ' Returns 3.4.
myResult = 12.6 Mod 5   ' Returns 2.6.
myResult = 47.9 Mod 9.35   ' Returns 1.15.

See Also

Arithmetic Operators | Operator Precedence in Visual Basic | Operators Listed by Functionality | Arithmetic Operators