Let’s go over the three most widely used floating-point number types in .NET so that we can choose the right type for our program.
A decimal variable contains a floating decimal point type. In other words, a number is represented in this number type using decimal numbers (0-9). To store and represent data, it uses 128 bits (16 bytes). That’s why the precision of the decimal number is higher than that of float and double, but mostly it is much slower than float and double. This is because decimal operations are done in base 10 while floating-point operations are done in binary. We use the decimal type in financial applications, mainly due to its high precision and to minimize rounding errors.
A float variable contains a floating binary point type. In other words, a number is represented in this number type in its binary form. To store and represent data, it uses 32 bits (4 bytes). The type is preferred for use in graphics libraries mainly due to the high demand for processing power. Furthermore, we can use it where preventing rounding errors is not very important since it is a single-precision (IEC 60559 format) data type.
A double variable also contains a floating binary point type but has double precision (IEC 60559 format). To store and represent data, it uses 64 bits (8 bytes). It is the data type that is mainly used for real values, but it is not recommended in situations in which we require high precision and accuracy.
decimal
: We can declare a decimal
number by using m
or M
as a suffix.decimal dec = 2.3E5m;
float
: We can declare a float
number by using f
or F
as a suffix.float fl = 240.62E-2f;
double
: Any fraction value is a double by default, but if we have to declare a float, we can use d
or D
as a suffix.double dbl = 0.85333e3;
class SampleClass{static void Main(){// Declare a decimal number, suffix mdecimal dec = 240.624545454545454545454545E-2m;// Declare a float number, suffix ffloat fl = 240.624545454545454545454545E-2f;// Fraction value is a double by defaultdouble dbl = 240.624545454545454545454545E-2;System.Console.WriteLine("240.624545454545454545454545E-2 as: ");System.Console.WriteLine("\tDecimal: " + dec);System.Console.WriteLine("\tFloat: " + fl);System.Console.WriteLine("\tDouble: " + dbl);// major difference among them is precision and accuracydec = 1M/3;fl = 1F/3;dbl = 1D/3;System.Console.WriteLine("--------------------------------------------------------------------------");System.Console.WriteLine("1/3 as:");System.Console.WriteLine("\tDecimal: " + dec); // 28-29 decimal placesSystem.Console.WriteLine("\tFloat: " + fl); // 7 significant digits of precisionSystem.Console.WriteLine("\tDouble: " + dbl); // 15-16 significant digits of precision}}
dec
, fl
and dbl
, respectively—and assign them values.Decimal | Float | Double |
---|---|---|
Data is represented in decimal using 128 bits. | Data is represented in float using 32 bits. | Data is represented in double using 64 bits. |
The range of decimal values is approximately ±1.0 to ±7.9. | The range of float values is approximately ±1.5 to ±3.4. | The range of double values is approximately ±5.0 to ±1.7. |
The precision of a decimal number is higher than both float and double. | The precision of a float number is lower than both double and decimal. | The precision of a double number is higher than float but lower than decimal. |
The accuracy of the decimal is higher than float and double. | The accuracy of the float is lower than decimal and double. | The accuracy of the double is higher than float but lower than decimal. |
Free Resources