csharp-tipos-decimales-float-double-decimal

Decimal Types in C# (Float, Double, and Decimal)

  • 7 min

Decimal numbers allow us to represent real numbers, that is, those that can have a fractional part and be positive or negative.

In C#, the main types for storing decimal numbers are float, double, and decimal.

Each has a specific range and precision, which must be considered depending on the type of calculations to be performed.

C# Type.NET TypeBitsPrecisionSuffixWhen to use it?
floatSingle32~7 digitsf3D Graphics, Unity, ML, Huge Arrays.
doubleDouble64~15 digitsdScience, Engineering, general use.
decimalDecimal128~28 digitsmMoney, Finance, Accounting.
  1. Are you calculating money, payroll, prices, or anything where losing a cent is illegal or a problem? Use decimal.
  2. Are you making a video game, processing audio, or working with neural networks? Use float.
  3. None of the above (general use, mathematics)? Use double (it’s the default standard in C#).

Floating Point: float and double

The float and double types are Binary Floating-Point types. They are designed following the IEEE 754 standard, which is the norm followed by almost all modern processors for fast mathematics.

Internally, they do not store the exact number. They store a scientific approximation based on three parts:

  1. Sign: Positive or negative.
  2. Mantissa: The significant digits of the number.
  3. Exponent: Where we place the decimal point (hence “floating point”, the point moves).

This allows representing astronomically large or microscopically small numbers, but at a cost: loss of precision.

float (single precision)

  • Size: 32 bits (4 bytes).
  • Precision: ~6-9 digits.
  • Suffix: f or F.
  • Use: 3D Graphics (Unity uses float for everything), physics engines, signal processing where speed is critical and a small error is acceptable.
float gravity = 9.81f; // Note the 'f' at the end
Copied!

double (double precision)

  • Size: 64 bits (8 bytes).
  • Precision: ~15-17 digits.
  • Suffix: d or D (optional, it’s the default).
  • Use: Scientific calculations, general mathematics.
double sunDistance = 149_600_000.5; // By default it's double
Copied!

In C#, any number with decimals you write in the code (literal), like 3.14, is automatically interpreted as double. If you want it to be float, **you have to add the f**.

The High-Precision Solution decimal

If float and double are fast but “imprecise”, decimal is the complete opposite.

The decimal type is a 128-bit Floating-Point type specifically designed for financial and monetary calculations. Unlike its siblings, decimal uses base 10 instead of base 2 for its fractional part.

This means numbers like are represented exactly.

  • Size: 128 bits (16 bytes).
  • Precision: 28-29 significant digits.
  • Suffix: m or M (for “Money”).
  • Range: Smaller than double, but much more precise.
  • Performance: Much slower (calculated by software, not hardware).
decimal price = 19.99m; // The 'm' is MANDATORY
decimal tax = 0.21m;
decimal total = price * (1 + tax); // Exact calculation, without strange rounding errors

Copied!

If you try to assign a double literal to a decimal without the m suffix, the compiler will give you an error. C# protects you from losing precision unintentionally.

Common Mathematical Operations

Arithmetic operations are similar to those for integers. We can perform addition, subtraction, multiplication, and division with floating-point numbers.

double x = 10.5;
double y = 3.2;

double sum = x + y;
double subtraction = x - y;
double multiplication = x * y;
double division = x / y;

Console.WriteLine($"Sum: {sum}");
Console.WriteLine($"Subtraction: {subtraction}");
Console.WriteLine($"Multiplication: {multiplication}");
Console.WriteLine($"Division: {division}");
Copied!

Practical Examples