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

C# Numeric Types (Number)

Generally, numbers can be divided into two types: integer types and floating-point types.

Integer typesare integers without a decimal point. They can be negative or positive.

Floating-point typesis a number with one or more decimal points. It can be negative or positive.

C# includes different data types for integer and floating-point types based on their size in memory and the ability to store numbers.

The following figure illustrates the numeric types in C#.

Numeric types

Integer types

Integer type numbers are positive or negative integers with a decimal point. C# includes four integer data types: byte, short, int, and long (byte, short, int, long).

byte

The byte data type stores integers from255number. It occupies8bit. The byte keyword is an alias for the .NET Byte structure.

same as byte, but it can store-128to127negative numbers between them. The sbyte keyword is an alias for the .NET SByte structure.

byte b1 = 255;
byte b2 = -128;// Compile-time error: constant value " -128" cannot be converted to "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

The short data type is a signed integer that can store-32,0768to32,0767between them. It occupies16bit memory. The short keyword is in .NET as part of the 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

unsigned integer. It can only store numbers from65535positive numbers between them. The ushort keyword is in .NET as part of the 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 = 35"000";//Compile-time error: constant value " 35"000" cannot be converted to "short"
ushort us1 = 65535;
ushort us2 = -32"000"; //Compile-time error: constant value " -32"000" cannot be converted to "ushort"
Console.WriteLine(Int16.MaxValue);//32767
Console.WriteLine(Int16.MinValue);//-32768
Console.WriteLine(UInt16.MaxValue);//65535
Console.WriteLine(UInt16.MinValue);//0

int

The int data type is32bit signed integer. It can store from-2,0147,0483,0648to2,0147,0483,0647number. The int keyword is in .NET as part of the 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 is32bit unsigned integer. The uint keyword is in .NET as part of the UInt32alias for a structure. It can store unsigned integers from4,0294,0967,0295positive number (optional). Append U or u suffix after the number to assign it to a uint variable.

int i = -2147483648;
int j = 2147483647;
int k = 4294967295; //Compile-time error: Cannot implicitly convert type 'uint' to 'int'.
uint ui1 = 4294967295;
uint ui2 =-1; //Compile-time error: constant value " -1" cannot be converted to " uint"
Console.WriteLine(Int32.MaxValue);//2147483647
Console.WriteLine(Int32.MinValue);//-2147483648
Console.WriteLine(UInt32.MaxValue);//4294967295
Console.WriteLine(UInt32.MinValue);//0

The int data type is also used for hexadecimal and binary numbers. Hexadecimal numbers start with the prefix 0x or 0X. From C#7.2Starting, binary numbers start with 0b or 0B.

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

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

Make it a floating-point type by adding the text f or F suffix.

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.

Make it a double-precision type by adding the text d or D suffix.

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, making it suitable for financial and monetary calculations.

Make it a decimal type by adding the text m or M suffix.

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

Scientific Notation

Use e or E to represent10The power, as the exponent part of scientific notation, is represented by floating-point numbers, double-precision numbers, or decimals.

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