What's the difference between decimal, float, and double in .NET?

Overview

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.

Decimal

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.

Float

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.

Double

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.

Syntax

  • 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;
    

Coding example

class SampleClass
{
static void Main()
{
// Declare a decimal number, suffix m
decimal dec = 240.624545454545454545454545E-2m;
// Declare a float number, suffix f
float fl = 240.624545454545454545454545E-2f;
// Fraction value is a double by default
double 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 accuracy
dec = 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 places
System.Console.WriteLine("\tFloat: " + fl); // 7 significant digits of precision
System.Console.WriteLine("\tDouble: " + dbl); // 15-16 significant digits of precision
}
}

Explanation

  • Lines 5–10: We declare decimal, float, and double type variables—dec, fl and dbl, respectively—and assign them values.
  • Lines 13–15: We print the assigned values.
  • Lines 18–20: We reassign the same value to each variable to see the difference in precision.
  • Lines 24–26: We print the value as stored in each variable.

Comparison between decimal, float, and double

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.0e28e^{-28} to ±7.9e28e^{28}. The range of float values is approximately ±1.5e45e^{-45} to ±3.4e38e^{38}. The range of double values is approximately ±5.0e324e^{-324} to ±1.7e308e^{308}.
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

Copyright ©2025 Educative, Inc. All rights reserved