/ Operator (Visual Basic)
Divides two numbers and returns a floating-point result.
expression1 / expression2
- expression1
Required. Any numeric expression.
- expression2
Required. Any numeric expression.
All numeric types, including the unsigned and floating-point types and Decimal.
The result is the full quotient of expression1 divided by expression2, including any remainder.
The \ Operator (Visual Basic) returns the integer quotient, which drops the remainder.
The data type of the result depends on the types of the operands. The following table shows how the data type of the result is determined.
Operand data types | Result data type |
---|---|
Both expressions are integral data types (SByte, Byte, Short, UShort, Integer, UInteger, Long, ULong) | Double |
One expression is a Single data type and the other is not a Double | Single |
One expression is a Decimal data type and the other is not a Single or a Double | Decimal |
Either expression is a Double data type | Double |
Before division is performed, any integral numeric expressions are widened to Double. If you assign the result to an integral data type, Visual Basic attempts to convert the result from Double to that type. This can throw an exception if the result does not fit in that type. In particular, see "Attempted Division by Zero" on this Help page.
If expression1 or expression2 evaluates to Nothing, it is treated as zero.
If expression2 evaluates to zero, the / operator behaves differently for different operand data types. The following table shows the possible behaviors.
Operand data types | Behavior if expression2 is zero |
---|---|
Floating-point (Single or Double) | Returns infinity (PositiveInfinity or NegativeInfinity), or NaN (not a number) if expression1 is also zero |
Decimal | Throws DivideByZeroException |
Integral (signed or unsigned) | Attempted conversion back to integral type throws OverflowException because integral types cannot accept PositiveInfinity, NegativeInfinity, or NaN |
Note |
---|
The / 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 (Visual Basic). |
Example
This example uses the / operator to perform floating-point division. The result is the quotient of the two operands.
Dim resultValue As Double resultValue = 10 / 4 resultValue = 10 / 3
The expressions in the preceding example return values of 2.5 and 3.333333. Note that the result is always floating-point (Double), even though both operands are integer constants.