Number data type c# | #MortalTech | #6



Number data type c# | #MortalTech | #6

Number data type c# | #MortalTech | #6

Number data type c# | #MortalTech | #6

Numbers, in general, can be divided into two types: Integer type and floating-point types.

Integer type numbers are whole numbers without decimal points. It can be negative or positive numbers.

Floating-point type is numbers with one or more decimal points. It can be negative or positive numbers.

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

The following figure illustrates numeric types in C#.
Byte
The byte data type stores numbers from 0 to 255. It occupies 8-bit in the memory. The byte keyword is an alias of the Byte struct in .NET.

The sbyte is the same as byte, but it can store negative numbers from -128 to 127. The sbyte keyword is an alias for SByte struct in .NET.

Short
The short data type is a signed integer that can store numbers from -32,768 to 32,767. It occupies 16-bit memory. The short keyword is an alias for Int16 struct in .NET.

The ushort data type is an unsigned integer. It can store only positive numbers from 0 to 65,535. The ushort keyword is an alias for UInt16 struct in .NET.

Int
The int data type is 32-bit signed integer. It can store numbers from -2,147,483,648 to 2,147,483,647. The int keyword is an alias of Int32 struct in .NET.

The uint is 32-bit unsigned integer. The uint keyword is an alias of UInt32 struct in .NET. It can store positive numbers from 0 to 4,294,967,295. Optionally use U or u suffix after a number to assign it to uint variable.

Long
The long type is 64-bit signed integers. It can store numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use l or L suffix with number to assign it to long type variable. The long keyword is an alias of Int64 struct in .NET.

The ulong type stores positive numbers from 0 to 18,446,744,073,709,551,615. If a number is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, its type is ulong. The ulong keyword is an alias of UInt64 struct in .NET.

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

Float
The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. It occupies 4 bytes in the memory. The float keyword is an alias of Single struct in .NET.

Use f or F suffix with literal to make it float type.

Double
The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. It occupies 8 bytes in the memory. The double keyword is an alias of the Double struct in .NET.

Use d or D suffix with literal to make it double type.

Decimal
The decimal data type can store fractional numbers from ±1.0 x 10-28 to ±7.9228 x 1028. It occupies 16 bytes in the memory. The decimal is a keyword alias of the Decimal struct in .NET.

The decimal type has more precision and a smaller range than both float and double, and so it is appropriate for financial and monetary calculations.

Use m or M suffix with literal to make it decimal type.

You can also find me here:
Instagram: MortalsTech
Facebook: MortalTech
Twitter: Mortalstech