C# Language float, double, decimal


Example

float

float is an alias to the .NET datatype System.Single. It allows IEEE 754 single-precision floating point numbers to be stored. This data type is present in mscorlib.dll which is implicitly referenced by every C# project when you create them.

Approximate range: -3.4 × 1038 to 3.4 × 1038

Decimal precision: 6-9 significant digits

Notation:

float f = 0.1259;
var f1 = 0.7895f; // f is literal suffix to represent float values 

It should be noted that the float type often results in significant rounding errors. In applications where precision is important, other data types should be considered.


double

double is an alias to the .NET datatype System.Double. It represents a double-precision 64-bit floating-point number. This datatype is present in mscorlib.dll which is implicitly referenced in any C# project.

Range: ±5.0 × 10−324 to ±1.7 × 10308

Decimal precision: 15-16 significant digits

Notation:

double distance = 200.34; // a double value
double salary = 245; // an integer implicitly type-casted to double value
var marks = 123.764D; // D is literal suffix to represent double values

decimal

decimal is an alias to the .NET datatype System.Decimal. It represents a keyword indicates a 128-bit data type. Compared to floating-point types, the decimal type has more precision and a smaller range, which makes it appropriate for financial and monetary calculations. This datatype is present in mscorlib.dll which is implicitly referenced in any C# project.

Range: -7.9 × 1028 to 7.9 × 1028

Decimal precision: 28-29 significant digits

Notation:

decimal payable = 152.25m; // a decimal value
var marks = 754.24m; // m is literal suffix to represent decimal values