English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tipos Númericos do C# (Number)

Normalmente, os números podem ser divididos em dois tipos: tipos de número inteiro e tipos de número de ponto flutuante.

Tipos de númeroÉ um número inteiro sem ponto flutuante. Ele pode ser negativo ou positivo.

Floating-point typesÉ um número com um ou mais pontos decimais. Ele pode ser negativo ou positivo.

O C# inclui diferentes tipos de dados para tipos de número inteiro e de ponto flutuante com base no tamanho que eles ocupam na memória e na capacidade de armazenamento de números.

A figura a seguir ilustra os tipos de número no C#.

Tipos numéricos

Tipos de número

Tipos de número inteiro

Tipos de número inteiro são números inteiros com ponto flutuante, positivos ou negativos. O C# inclui quatro tipos de dados para números inteiros: byte, short, int, long (byte, short, int, long).

byte255O tipo de dados byte armazena números inteiros de 0 a8número. Ele ocupa

bits. A palavra-chave byte é o alias da estrutura Byte do .NET.-128to127Entre os números negativos. O sbyte é o alias da estrutura SByte do .NET.

byte b1 = 255;
byte b2 = -128;// uint ui -128"Não pode ser convertido para "byte""
sbyte sb1 = -128; 
sbyte sb2 = 127; 
Console.WriteLine(Byte.MaxValue);//255
Console.WriteLine(Byte.MinValue);//0
Console.WriteLine(SByte.MaxValue);//127
Console.WriteLine(SByte.MinValue);//-128

short

O tipo de dados short é inteiro assinado, pode armazenar-32,0768to32,0767entre os números. Ele ocupa16bits de memória. A palavra-chave short é do .NET Int16is an alias for the structure. If the number suffix is UL, Ul, uL, ul, LU, Lu, lU or lu, then its type is ulong. The uint keyword is an alias for the UInt

O tipo de dados ushort é inteiro sem sinal. Ele pode armazenar de 0 a65535entre os números positivos. A palavra-chave ushort é do .NET UInt16is an alias for the structure. If the number suffix is UL, Ul, uL, ul, LU, Lu, lU or lu, then its type is ulong. The uint keyword is an alias for the UInt

short s1 = -32768;
short s2 = 32767;
short s3 = 35000;//uint ui 35"000" não pode ser convertido para "short"
ushort us1 = 65535;
ushort us2 = -32000; //uint ui -32"000" não pode ser convertido para "ushort"
Console.WriteLine(Int16.MaxValue);//32767
Console.WriteLine(Int16.MinValue);//-32768
Console.WriteLine(UInt16.MaxValue);//65535
Console.WriteLine(UInt16.MinValue);//0

int

O tipo de dados int é32long type is-2,0147,0483,0648to2,0147,0483,0647número. A palavra-chave int é do .NET Int32is an alias for the structure. If the number suffix is UL, Ul, uL, ul, LU, Lu, lU or lu, then its type is ulong. The uint keyword is an alias for the UInt

uint é32bits de inteiro sem sinal. A palavra-chave uint é do .NET UInt32O alias da estrutura. Pode armazenar de 0 a4,0294,0967,0295O número positivo. (Opcional) Use o sufixo U ou u após o número para atribuí-lo a uma variável uint.

int i = -2147483648;
int j = 2147483647;
int k = 4294967295; //Erro de compilação: Não é possível converter implicitamente o tipo 'uint' para 'int'.
uint ui1 = 4294967295;
uint ui2 =-1; //uint ui -1Compile-time error: constant value “
Console.WriteLine(Int32.MaxValue);//2147483647
Console.WriteLine(Int32.MinValue);//-2147483648
Console.WriteLine(UInt32.MaxValue);//4294967295
Console.WriteLine(UInt32.MinValue);//0

cannot be converted to “uint”7.2“”

Example: hexadecimal, binary2F;
int hex = 0x10int binary = 0b_001111;
_
Console.WriteLine(hex);

Console.WriteLine(binary);

long64long type is-9,0223,0372The ulong type stores numbers from 0 to36,0854,0775,0808to9,0223,0372The ulong type stores numbers from 0 to36,0854,0775,0807bit signed integer. It can store from64is an alias for the structure. If the number suffix is UL, Ul, uL, ul, LU, Lu, lU or lu, then its type is ulong. The uint keyword is an alias for the UInt

. Use the numeric suffix l or L to assign it to a long type variable. The long keyword is an alias for the Int18,0446,0744The ulong type stores numbers from 0 to73,0709,0551,0615,64is an alias for the structure. If the number suffix is UL, Ul, uL, ul, LU, Lu, lU or lu, then its type is ulong. The uint keyword is an alias for the UInt

long l1 = -9223372036854775808;
long l2 = 9223372036854775807;
ulong ul1 = 18223372036854775808ul;
ulong ul2 = 18223372036854775808UL;
Console.WriteLine(Int64.MaxValue);//9223372036854775807
Console.WriteLine(Int64.MinValue);//-9223372036854775808
Console.WriteLine(UInt64.MaxValue);//18446744073709551615
Console.WriteLine(UInt64.MinValue);//0

Floating-point types

Floating-point numbers are positive or negative numbers with one or more decimal points. C# includes three floating-point number data types: float, double, and decimal (float, double, decimal).

float

The float data type can store from3.4ee038to3.4e + 038fraction. It occupies4bytes. The float keyword is an alias for the Single structure in .NET.

Use the text suffix f or F to make it a floating point type.

float f1 = 123456.5F;
float f2 = 1.123456f;
Console.WriteLine(f1);//123456.5
Console.WriteLine(f2);//1.123456

double

The double data type can store from1.7eˆ308to1.7e + 308decimal. It occupies8bytes. The double keyword is an alias for the Double structure in .NET.

Use the text suffix d or D to make it a double precision type.

double d1 = 12345678912345.5d;
double d2 = 1.123456789123456d;
Console.WriteLine(d1);//12345678912345.5
Console.WriteLine(d2);//1.123456789123456

decimal

The decimal data type can store from ±1.0 x 10-28to ±7.9228 x 1028decimal. It occupies16bytes. Decimal is an alias for the Decimal structure in .NET.

The decimal type has higher precision and a smaller range than floating point and double precision types, so it is suitable for financial and monetary calculations.

Use the m or M suffix with text to make it a decimal type.

decimal d1 = 123456789123456789123456789.5m;
decimal d2 = 1.1234567891345679123456789123m;
Console.WriteLine(d1);
Console.WriteLine(d2);

Número Científico

Usar e ou E para representar10A potência, como parte do expoente do número científico, é representada por números de ponto flutuante, números de precisão dupla ou decimais.

double d = 0.12e2;
Console.WriteLine(d);  // 12;
float f = 123.45e-2f;
Console.WriteLine(f);  // 1.2345
decimal m = 1.2e6m;
Console.WriteLine(m);// 1200000