0
0
CsharpComparisonBeginner · 4 min read

Float vs Double vs Decimal in C#: Key Differences and Usage

In C#, float is a 32-bit single-precision floating-point type, double is a 64-bit double-precision floating-point type, and decimal is a 128-bit type designed for high-precision decimal values. float and double are good for scientific calculations, while decimal is best for financial and monetary calculations requiring exact decimal representation.
⚖️

Quick Comparison

Here is a quick overview comparing float, double, and decimal in C# based on key factors.

Factorfloatdoubledecimal
Size (bits)3264128
Precision (approx.)7 digits15-16 digits28-29 digits
TypeSingle-precision floating-pointDouble-precision floating-point128-bit decimal floating-point
Use CaseGraphics, games, less precise calculationsScientific calculations, general useFinancial, monetary calculations needing exact decimals
Range±1.5 × 10⁻⁴⁵ to ±3.4 × 10³⁸±5.0 × 10⁻³²⁴ to ±1.7 × 10³⁰⁸±1.0 × 10⁻²⁸ to ±7.9 × 10²⁸
Default Literal Suffixf or Fd or D (optional)m or M
⚖️

Key Differences

float and double are binary floating-point types that store numbers in base 2, which can cause small rounding errors for decimal fractions. float uses 32 bits and offers less precision, making it suitable for applications where memory is limited or precision is less critical.

double uses 64 bits and provides more precision and a wider range, making it the default choice for floating-point calculations in C#.

decimal is a 128-bit decimal floating-point type designed to store decimal numbers exactly, avoiding rounding errors common in binary floating-point types. This makes it ideal for financial and monetary calculations where exact decimal representation is required. However, decimal is slower and uses more memory than float or double.

⚖️

Code Comparison

csharp
using System;

class Program
{
    static void Main()
    {
        float f = 1.2345678f;
        Console.WriteLine($"float value: {f}");

        double d = 1.23456789012345;
        Console.WriteLine($"double value: {d}");

        decimal m = 1.2345678901234567890123456789m;
        Console.WriteLine($"decimal value: {m}");
    }
}
Output
float value: 1.234568 double value: 1.23456789012345 decimal value: 1.2345678901234567890123456789
↔️

Decimal Equivalent

csharp
using System;

class Program
{
    static void Main()
    {
        decimal m = 1.2345678901234567890123456789m;
        Console.WriteLine($"decimal value: {m}");

        double d = (double)m;
        Console.WriteLine($"converted to double: {d}");

        float f = (float)m;
        Console.WriteLine($"converted to float: {f}");
    }
}
Output
decimal value: 1.2345678901234567890123456789 converted to double: 1.23456789012346 converted to float: 1.234568
🎯

When to Use Which

Choose float when you need to save memory and precision is not critical, such as in graphics or simple scientific calculations.

Choose double for most general-purpose floating-point calculations where you need more precision and range than float offers.

Choose decimal when you need exact decimal representation, especially for financial or monetary calculations where rounding errors are unacceptable.

Key Takeaways

float is 32-bit with ~7 digits precision, good for less precise needs.
double is 64-bit with ~15-16 digits precision, the default for floating-point math.
decimal is 128-bit with ~28-29 digits precision, best for exact decimal values.
Use decimal for money to avoid rounding errors common in binary floats.
Choose the type based on precision needs, memory, and performance trade-offs.