Decimal vs Double in C#: What to Choose and When? Guide 2025

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Decimal vs Double in C#: The Battle for Precision (and Your Sanity)



Have you ever written a salary calculator or, say, a ball flight simulator, and suddenly got 0.30000000000000004 instead of the expected 0.3? If so — welcome to the club. The question "decimal vs double! - Which one should I use and when?" is one of the most popular on Stack Overflow, and for good reason. It seems like a simple choice between two floating-point number types. But in reality, it's a choice between a financial disaster and scientific precision (or vice versa).



In this article, we'll settle this eternal debate once and for all. You'll learn how the decimal type in C# differs from the double type, where to use each one, and most importantly — how not to mess up in 2025. Let's go!



The Gist: What's Under the Hood?



Before we start throwing arguments around, let's look at the specification. Each type has its own architecture, and that's what determines where it will be king and where it will be an outcast.



Double (aka System.Double)

  • Size: 64 bits (8 bytes).
  • Precision: Approximately 15-17 significant digits.
  • Range: Huge (from ±5.0 × 10−324 to ±1.7 × 10308).
  • Representation: Floating-point according to IEEE 754 standard (binary).
  • Performance: High. Processors are optimized for working with double.


Decimal (aka System.Decimal)

  • Size: 128 bits (16 bytes).
  • Precision: 28-29 significant digits.
  • Range: Smaller (from ±1.0 × 10−28 to ±7.9 × 1028).
  • Representation: Decimal (essentially an integer with a decimal point shift).
  • Performance: Lower. No hardware support on the CPU; all calculations are software-based.


The key difference to remember: double is a binary fraction, while decimal is a decimal fraction. This is precisely what causes the famous "rounding errors".



Why Does double "Break" Money but Is Ideal for Science?



Imagine trying to write the number 0.1 in binary. It would be an infinite repeating fraction: 0.0001100110011.... The computer truncates it, resulting in a small but critical error. For scientific calculations (weather simulations, physics, graphics), this error accumulates but stays within acceptable limits. For finance — it's a "stolen" penny from a million users.



Decimal, on the other hand, stores the number as an integer (for example, 1/10 = 0.1 is stored as the integer 1 and information that the decimal point needs to be shifted by one place). Therefore, decimal precision is ideal for decimal fractions.



When to Use decimal in C#? (Spoiler: For Money)



The golden rule that will save your project: use decimal for financial, monetary, and accounting calculations. If your variable holds rubles, dollars, loan interest, or VAT — your choice is clear.



Here's an example of why double is evil for your wallet:



using System;

public class MoneyExample{ public static void Main() { // Bad example with double double price = 10.99; double tax = 0.07; double totalDouble = price + (price * tax); Console.WriteLine($"Double total: {totalDouble}"); // Might output 11.759300000000001

// Good example with decimal decimal priceDec = 10.99m; decimal taxDec = 0.07m; decimal totalDec = priceDec + (priceDec * taxDec); Console.WriteLine($"Decimal total: {totalDec}"); // Exactly: 11.7593 }}


See that "extra

Blogs

Book Recommendations