Export (0) Print
Expand All

8.3 Numeric Conversions

Visual Studio .NET 2003

Numeric conversions are conversions between the types Byte, Short, Integer, Long, Decimal, Single and Double, and enumerated types. Enumerated types are treated as if they were their underlying types for the purpose of conversions. When converting from a numeric type to an enumerated type, the numeric type is never required to conform to the set of values defined in the enumerated type.

Numeric conversions are processed at run-time as follows:

  • For a conversion from a numeric type to a wider numeric type, the value is simply converted to the wider type. Conversions from Integer, Long, or Decimal to Single or Double are rounded to the nearest Single or Double value. While this conversion may cause a loss of precision, it will never cause a loss of magnitude.
  • For a conversion from an integral type to a smaller integral type, or from Single, Double, or Decimal to an integral type, the result depends on whether integer overflow checking is on:

    If integer overflow is being checked:

    • If the source is an integral type, the conversion succeeds if the source argument is within the range of the destination type. The conversion throws a System.OverflowException exception if the source argument is outside the range of the destination type.
    • If the source is Single, Double, or Decimal, the source value is rounded up or down to the nearest integral value, and this integral value becomes the result of the conversion. If the source value is equally close to two integral values, the value is rounded to the value that has an even number in the least significant digit position. If the resulting integral value is outside the range of the destination type, a System.OverflowException exception is thrown.

    If integer overflow is not being checked:

    • If the source is an integral type, the conversion always succeeds and simply consists of discarding the most significant bits of the source value.
    • If the source is Single, Double, or Decimal, the conversion always succeeds and simply consists of rounding the source value towards the nearest integral value. If the source value is equally close to two integral values, the value is always rounded to the value that has an even number in the least significant digit position.
    • For a conversion from Double to Single, the Double value is rounded to the nearest Single value. If the Double value is too small to represent as a Single, the result becomes positive zero or negative zero. If the Double value is too large to represent as a Single, the result becomes positive infinity or negative infinity. If the Double value is NaN, the result is also NaN.
  • For a conversion from Single or Double to Decimal, the source value is converted to Decimal representation and rounded to the nearest number after the 28th decimal place if required. If the source value is too small to represent as a Decimal, the result becomes zero. If the source value is NaN, infinity, or too large to represent as a Decimal, a System.OverflowException exception is thrown.
  • For a conversion from Double to Single, the Double value is rounded to the nearest Single value. If the Double value is too small to represent as a Single, the result becomes positive zero or negative zero. If the Double value is too large to represent as a Single, the result becomes positive infinity or negative infinity. If the Double value is NaN, the result is also NaN.

See Also

8.5 Array Covariance | 8.8 Widening Conversions | 8.9 Narrowing Conversions | 8. Conversions | Conversion Summary (Visual Basic Language Reference) | Implicit and Explicit Conversions (Visual Basic Language Concepts)

Show:
© 2014 Microsoft