Compartilhar via


Tipos de dados numéricos (Visual Basic)

Visual Basic supplies several numeric data types for handling numbers in various representations. Integral types represent only whole numbers (positive, negative, and zero), and nonintegral types represent numbers with both integer and fractional parts.

For a table showing a side-by-side comparison of the Visual Basic data types, see Resumo de tipo de dados (Visual Basic).

Integral Numeric Types

Integral data types are those that represent only numbers without fractional parts.

The signed integral data types are Tipo de dado SByte (Visual Basic) (8-bit), Tipo de dados Short (Visual Basic) (16-bit), Tipo de Dados Inteiro (Visual Basic) (32-bit), and Tipo de dados Long (Visual Basic) (64-bit). If a variable always stores integers rather than fractional numbers, declare it as one of these types.

The unsigned integral types are Tipo de dados Byte (Visual Basic) (8-bit), Tipo de dados UShort (Visual Basic) (16-bit), Tipo de dados UInteger (32-bit), and Tipo de dados ULong (Visual Basic) (64-bit). If a variable contains binary data, or data of unknown nature, declare it as one of these types.

Performance

Arithmetic operations are faster with integral types than with other data types. They are fastest with the Integer and UInteger types in Visual Basic.

Large Integers

Se você precisa manter um inteiro maior que o Integer tipo de dados pode comportar, você pode usar o Long tipo de dados em vez disso. Longvariáveis podem conter números de -9.223.372.036.854.775.808 através de 9.223.372.036.854.775.807. Operações com Long são um pouco mais lento do que com Integer.

If you need even larger values, you can use the Tipo de dados decimais (Visual Basic). You can hold numbers from -79,228,162,514,264,337,593,543,950,335 through 79,228,162,514,264,337,593,543,950,335 in a Decimal variable if you do not use any decimal places. However, operations with Decimal numbers are considerably slower than with any other numeric data type.

Small Integers

Se você não precisar de toda a gama da Integer tipo de dados, você pode usar o Short tipo de dados, que pode conter números inteiros de -32.768 até 32.767. Para o menor intervalo inteiro , o SByte tipo de dados contém inteiros de -128 a 127. Se você tiver um grande número de variáveis que armazenar números inteiros pequenos, o Common Language Runtime , às vezes, pode armazenar seu Short e SByte variáveis de maneira mais eficiente e economizar o consumo de memória . However, operations with Short and SByte are somewhat slower than with Integer.

Unsigned Integers

If you know that your variable never needs to hold a negative number, you can use the unsigned types Byte, UShort, UInteger, and ULong. Each of these data types can hold a positive integer twice as large as its corresponding signed type (SByte, Short, Integer, and Long). In terms of performance, each unsigned type is exactly as efficient as its corresponding signed type. In particular, UInteger shares with Integer the distinction of being the most efficient of all the elementary numeric data types.

Nonintegral Numeric Types

Nonintegral data types are those that represent numbers with both integer and fractional parts.

The nonintegral numeric data types are Decimal (128-bit fixed point), Tipo de dados único (Visual Basic) (32-bit floating point), and Tipo de dados duplo (Visual Basic) (64-bit floating point). They are all signed types. If a variable can contain a fraction, declare it as one of these types.

Decimalnão é um flutuante-ponto tipo de dados. Decimalnúmeros têm um valor bináriodeinteiro e um inteiro fator que especifica qual parte do valor é uma fração decimal de escala.

Floating-point (Single and Double) numbers have larger ranges than Decimal numbers but can be subject to rounding errors. Floating-point types support fewer significant digits than Decimal but can represent values of greater magnitude.

Nonintegral number values can be expressed as mmmEeee, in which mmm is the mantissa (the significant digits) and eee is the exponent (a power of 10). The highest positive values of the nonintegral types are 7.9228162514264337593543950335E+28 for Decimal, 3.4028235E+38 for Single, and 1.79769313486231570E+308 for Double.

Performance

Double is the most efficient of the fractional data types, because the processors on current platforms perform floating-point operations in double precision. However, operations with Double are not as fast as with the integral types such as Integer.

Small Magnitudes

For numbers with the smallest possible magnitude (closest to 0), Double variables can hold numbers as small as -4.94065645841246544E-324 for negative values and 4.94065645841246544E-324 for positive values.

Small Fractional Numbers

Se você não precisar de toda a gama da Double tipo de dados, você pode usar o Single tipo de dados, que pode conter números de ponto flutuante do-de - 3.4028235E + 38 a 3.4028235E + 38. As magnitudes de menores para Single variáveis são - 1, 401298E-45 para valores negativos e 1, 401298E-45 para valores positivos. If you have a very large number of variables that hold small floating-point numbers, the common language runtime can sometimes store your Single variables more efficiently and save memory consumption.

Consulte também

Tarefas

Solucionando problemas de tipos de dados (Visual Basic)

Como: Armazenar inteiros em uma variável (Visual Basic)

Como: Armazenar frações em uma variável (Visual Basic)

Como: Armazenar o maior número possível em uma variável (Visual Basic)

Como: otimizar o armazenamento de inteiros positivos com tipos sem-sinal (Visual Basic)

Como: Chamar uma função do Windows que obtém tipos sem-sinal (Visual Basic)

Como: Armazenar o máximo de dígitos significativos em uma variável (Visual Basic)

Como: Armazenar valores monetários em uma variável (Visual Basic)

Conceitos

Tipos de dados de caractere (Visual Basic)

Diversos tipos de dados (Visual Basic)

Outros recursos

Tipos de dados elementares (Visual Basic)