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

Tipos de números de C# (Number)

Por lo general, los números se pueden dividir en dos tipos: tipos de números enteros y tipos de números de punto flotante.

Tipos de números enteroses un número entero sin punto decimal. Puede ser negativo o positivo.

Floating-point typeses un número con uno o más puntos decimales. Puede ser negativo o positivo.

C# incluye diferentes tipos de datos para enteros y tipos de punto flotante según su tamaño en memoria y la capacidad de almacenar números.

La siguiente imagen ilustra los tipos de números en C#.

Tipos de números

Tipos de números enteros

Los tipos de números enteros son números enteros con punto decimal positivo o negativo. C# incluye cuatro tipos de datos enteros: byte, short, int, long (byte, short, int, long).

byte

El tipo de datos byte almacena desde 0 hasta255números. Ocupa8bits. La palabra clave byte es el alias de la estructura Byte en .NET.

es igual a byte, pero puede almacenar-128to127entre números negativos. La palabra clave sbyte es el alias de la estructura SByte en .NET.

byte b1 = 255;
byte b2 = -128;// uint ui -128"no se puede convertir a "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

El tipo de datos short es un entero con signo. Puede almacenar-32,0768to32,0767entre números. 占用16bits de memoria. La palabra clave short es en .NET Int16alias. The positive number. 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

El tipo de datos ushort es un entero sin signo. Puede almacenar desde 0 hasta65535entre números positivos. La palabra clave ushort es en .NET UInt16alias. The positive number. 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" no se puede convertir a "short"
ushort us1 = 65535;
ushort us2 = -32000; //uint ui -32"000" no se puede convertir a "ushort"
Console.WriteLine(Int16.MaxValue);//32767
Console.WriteLine(Int16.MinValue);//-32768
Console.WriteLine(UInt16.MaxValue);//65535
Console.WriteLine(UInt16.MinValue);//0

int

El tipo de datos int es32bit signed integer. It can store from-2,0147,0483,0648to2,0147,0483,0647número. La palabra clave int es en .NET Int32alias. The positive number. 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 es32enteros sin signo. La palabra clave uint es en .NET UInt32un alias para la estructura. Puede almacenar desde 0 hasta4,0294,0967,0295entero positivo. (Opcional) Después del número, utilice el sufijo U o u para asignarlo a una variable uint.

int i = -2147483648;
int j = 2147483647;
int k = 4294967295; //Error de compilación: no se puede convertir implícitamente el tipo 'uint' a '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.2Start, binary numbers start with 0b or 0B. The int data type is also used for hexadecimal and binary numbers. Hexadecimal numbers start with the prefix 0x or 0X. From C#

int hex = 0x2F;
int binary = 0b_0010_1111;
Console.WriteLine(hex);
Console.WriteLine(binary);

long

The long type is64bit signed integer. It can store from-9,0223,037236,0854,0775,0808to9,0223,037236,0854,0775,0807The long keyword is an alias for the Int64alias. The positive number. 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

The ulong type stores numbers from 0 to18,0446,074473,0709,0551,0615,64alias. The positive number. 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 keyword 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);

Notación científica

Se utiliza e o E para representar10potencia, como parte del exponente de la notación científica, se utiliza número flotante, número de precisión doble o decimal.

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