Question
I often see developers using double in C#. I have read that double can lose precision in some cases.
When should I use double, and when should I use decimal?
Which type is appropriate for money-related calculations, including large amounts such as values greater than 100 million?
Short Answer
By the end of this page, you will understand the practical difference between double and decimal in C#, why precision problems happen, and how to choose the right type for scientific values, measurements, and financial calculations. You will also see why decimal is usually the correct choice for money.
Concept
In C#, both double and decimal store numbers with fractional parts, but they are designed for different goals.
doubleis a binary floating-point type.decimalis a base-10 floating-point type with higher decimal precision.
This difference matters because many decimal values that look simple to humans cannot be represented exactly in binary.
For example, 0.1 is easy to write in decimal, but in binary floating-point it becomes an approximation. That means calculations with double can produce tiny rounding errors.
double a = 0.1;
double b = 0.2;
Console.WriteLine(a + b); // may display 0.30000000000000004
decimal is designed to represent decimal fractions much more accurately for business-style calculations.
decimal a = 0.1m;
decimal b = 0.2m;
Console.WriteLine(a + b); // 0.3
Why this matters
Choosing the wrong type can cause subtle bugs:
Mental Model
Think of double and decimal as two different measuring tools.
doubleis like a fast scientific calculator. It is great for physics, engineering, and approximations, but it may not show every decimal amount exactly.decimalis like an accountant’s ledger. It is built for numbers written in base 10, so values like10.25or199.99are handled much more naturally.
If you are tracking money, you want the accountant’s ledger.
If you are calculating the path of a rocket or the position of a character in a game, the scientific calculator is usually the better tool.
Syntax and Examples
Basic syntax
double temperature = 23.5;
decimal price = 19.99m;
Notice the m suffix for decimal literals. Without it, a fractional literal like 19.99 is treated as a double by default.
Example: double for measurement
double width = 5.1;
double height = 3.2;
double area = width * height;
Console.WriteLine(area);
This is a good use of double because measurements often involve approximation anyway.
Example: decimal for money
decimal price = 19.99m;
decimal tax = 1.50m;
decimal total = price + tax;
Console.WriteLine(total); // 21.49
This is a good use of because money values should be represented accurately in decimal form.
Step by Step Execution
Consider this example:
decimal subtotal = 99.95m;
decimal tax = 8.00m;
decimal total = subtotal + tax;
Console.WriteLine(total);
Step by step:
subtotalis created as adecimalwith value99.95.taxis created as adecimalwith value8.00.totalis calculated by adding the two decimal values.- The result is stored as
107.95. Console.WriteLine(total)prints107.95.
Now compare that with a double example:
double a = 0.1;
double b = 0.2;
double result = a + b;
Console.WriteLine(result);
Step by step:
Real World Use Cases
Use decimal when working with
- Product prices in an e-commerce app
- Bank balances and transactions
- Payroll calculations
- Tax and invoice totals
- Accounting reports
- Currency conversion results that must be rounded carefully
Example:
decimal unitPrice = 249.99m;
decimal quantity = 3;
decimal total = unitPrice * quantity;
Use double when working with
- Sensor readings
- Physics simulations
- Image processing
- Geometric calculations
- Statistical computations
- Game development coordinates
Example:
double latitude = 40.7128;
double longitude = -74.0060;
Large monetary values
Amounts greater than 100 million are still commonly stored as decimal in business systems. The value being large is not the main issue. The important issue is whether you need exact decimal precision. For money, the answer is usually yes.
Real Codebase Usage
In real C# projects, developers usually choose one numeric type based on the domain.
Common patterns with decimal
Validation and business rules
decimal amount = 150000000.00m;
if (amount < 0)
{
throw new ArgumentException("Amount cannot be negative.");
}
Calculating totals
decimal subtotal = 120.00m;
decimal discount = 20.00m;
decimal total = subtotal - discount;
Rounding for display or billing
decimal tax = 12.3456m;
decimal roundedTax = Math.Round(tax, 2);
Common patterns with double
Math-heavy code
double radius = 4.5;
double area = Math.PI * radius * radius;
Tolerance-based comparisons
Common Mistakes
1. Using double for money
Broken example:
double price = 10.10;
double tax = 0.20;
double total = price + tax;
Why it is a problem:
- You may get tiny precision errors.
- Those errors can accumulate across many calculations.
Better:
decimal price = 10.10m;
decimal tax = 0.20m;
decimal total = price + tax;
2. Forgetting the m suffix for decimal literals
Broken example:
decimal price = 19.99;
This fails because 19.99 is treated as a double literal.
Correct version:
decimal price = 19.99m;
3. Comparing values with
Comparisons
| Feature | double | decimal |
|---|---|---|
| Internal style | Binary floating-point | Decimal floating-point |
| Decimal precision | Approximate for many decimal fractions | Better suited for exact decimal fractions |
| Typical use | Science, graphics, measurements | Money, finance, accounting |
| Speed | Usually faster | Usually slower |
| Range | Wider | Smaller than double |
| Memory | 8 bytes | 16 bytes |
Good for 0.1 + 0.2 == 0.3 | No, not reliably | Yes, for decimal literals |
Cheat Sheet
Quick rules
- Use
decimalfor money. - Use
doublefor scientific and measurement-based calculations. - Use
msuffix for decimal literals. - Do not compare
doublevalues with==unless you truly know it is safe. - Do not mix
decimalanddoublein the same expression without explicit conversion.
Syntax
double distance = 12.5;
decimal price = 12.5m;
Good examples
decimal salary = 125000000.00m;
double angle = 45.5;
Dangerous example
double total = 0.1 + 0.2;
Better for money
total = m + m;
FAQ
Should I use decimal for all numbers in C#?
No. Use decimal when decimal precision is important, especially for money. Use double for general math, measurements, simulations, and scientific calculations.
Why is double not ideal for currency?
Because it stores values in binary floating-point, many decimal fractions cannot be represented exactly. That can introduce rounding errors.
Is decimal always more accurate than double?
Not in every possible sense. decimal is better for decimal fractions such as prices and totals. double has a wider range and is often better for scientific computing.
What should I use for values over 100 million dollars?
Usually decimal, because the main concern in financial software is decimal accuracy, not just the size of the number.
Why do decimal numbers need an m suffix in C#?
Because numeric literals with fractional parts are treated as double by default. The m tells C# to treat the literal as decimal.
Can I convert between and ?
Mini Project
Description
Build a simple invoice total calculator in C#. This project shows why decimal is the correct choice for financial calculations. The program will calculate a subtotal, tax, discount, and final total using money values.
Goal
Create a small C# program that calculates an invoice total accurately using decimal.
Requirements
Requirement 1 Requirement 2 Requirement 3 Requirement 4
Keep learning
Related questions
AddTransient vs AddScoped vs AddSingleton in ASP.NET Core Dependency Injection
Learn the differences between AddTransient, AddScoped, and AddSingleton in ASP.NET Core DI with examples and practical usage.
Best Way to Repeat a Character in C#: Building Repeated Strings Efficiently
Learn the best way to repeat a character in C#, compare StringBuilder, string concatenation, and simpler built-in options.
C# Array Initialization Syntaxes Explained
Learn all common C# array initialization syntaxes with examples, rules, comparisons, and mistakes beginners often make.