Udostępnij za pośrednictwem


How to: Hold the Largest Possible Number in a Variable

A variable holds the largest possible numbers with precision if you declare it to be of data type Decimal. The next largest integer capacity is data type ULong. If you do not need the precision of integral data types, you can use floating-point types for even greater magnitude.

Two Different Meanings for "Largest"

Largest Precise Value. If you need to hold large integers with complete precision down to the units digit, you can use the Decimal Data Type (Visual Basic). The Decimal type can hold integers from -79,228,162,514,264,337,593,543,950,335 through 79,228,162,514,264,337,593,543,950,335 (7.9...E+28).

Greatest Magnitude. The fractional types Single and Double can hold numbers of greater magnitude, but not with exact precision. The Single Data Type (Visual Basic) provides 8 digits of precision, and the Double Data Type (Visual Basic) provides 18 digits of precision.

To hold the largest possible integers in a variable

  1. Declare the variable with a Dim Statement (Visual Basic).

  2. Follow the variable name with an As clause, specifying the Decimal keyword.

    Dim atomsInTheUniverse As Decimal
    

Efficient Data Types

The Decimal type has the slowest performance of all the elementary numeric data types. If your integer numbers do not attain such large values and are always positive or zero, consider the ULong type.

A variable of the ULong Data Type (Visual Basic) can hold integers from 0 through 18,446,744,073,709,551,615 (1.8...E+19). Operations with ULong numbers are much faster than with Decimal, although not quite as efficient as with UInteger.

To hold large nonnegative integers in a variable with efficient performance

  1. Declare the variable with a Dim statement.

  2. Follow the variable name with an As clause, specifying the ULong keyword.

    Dim atomsInTheEarth As ULong
    

See Also

Concepts

Type Characters

Reference

Data Type Summary (Visual Basic)

Integer Data Type (Visual Basic)

Long Data Type (Visual Basic)

Decimal Data Type (Visual Basic)

UInteger Data Type

ULong Data Type (Visual Basic)

Other Resources

Elementary Data Types