Operador / (Visual Basic)

Divides two numbers and returns a floating-point result.

expression1 / expression2

Parts

  • expression1
    Required. Any numeric expression.

  • expression2
    Required. Any numeric expression.

Supported Types

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

Result

The result is the full quotient of expression1 divided by expression2, including any remainder.

The Operador \ (Visual Basic) returns the integer quotient, which drops the remainder.

Comentários

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

Uma expressão é um único tipo de dados e o outro não é um duplo

Single

Uma expressão é um Decimal tipo de dados e o outro não é um único ou um duplo

Decimal

Qualquer expressão for uma Double tipo de dados

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.

Se expression1 ou expression2 for avaliada como nada, ela é tratada como zero.

Attempted Division by 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

ObservaçãoObservação

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 Procedimentos de operador (Visual Basic).

Exemplo

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.

Consulte também

Referência

Operador /= (Visual Basic)

Operador \ (Visual Basic)

Tipos de dados de resultados de operador (Visual Basic)

Operadores Aritméticos (Visual Basic)

Precedência de operadores no Visual Basic

Operadores listados por Funcionalidade (Visual Basic)

Conceitos

Operadores aritméticos em Visual Basic