Divides two numbers and returns only the remainder.
number1 Mod number2
The result is the remainder left after division is performed on number1 and number2.
Byte, Short, Integer, Long, Single, Double, Decimal
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
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 = 10Mod
5 ' Returns 0. myResult = 10Mod
3 ' Returns 1. myResult = 12Mod
4.3 ' Returns 3.4. myResult = 12.6Mod
5 ' Returns 2.6. myResult = 47.9Mod
9.35 ' Returns 1.15.
Arithmetic Operators | Operator Precedence in Visual Basic | Operators Listed by Functionality |