Question

decimal vs double in C#: When to Use Each Type

csharpdoubledecimalprecisioncurrency

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.

  • double is a binary floating-point type.
  • decimal is 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:

  • Financial totals may be off by a cent or more.
  • Equality comparisons may fail unexpectedly.
  • Reports and invoices may not match expected values.
  • Scientific or graphics code may need speed and range more than exact decimal precision.

General rule

  • Use decimal for money, prices, taxes, invoices, balances, interest rates, and anything where decimal accuracy matters.
  • Use double for scientific calculations, measurements, simulations, graphics, game math, and very large or very small values where performance and range matter more than exact decimal representation.

For money computations, including values greater than 100 million, decimal is still the suitable choice in normal business applications because precision is usually more important than raw speed.

Mental Model

Think of double and decimal as two different measuring tools.

  • double is like a fast scientific calculator. It is great for physics, engineering, and approximations, but it may not show every decimal amount exactly.
  • decimal is like an accountant’s ledger. It is built for numbers written in base 10, so values like 10.25 or 199.99 are 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.

Take Quiz

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 decimal because money values should be represented accurately in decimal form.

Example: why double can be surprising

double x = 0.1;
double y = 0.2;
double sum = x + y;

Console.WriteLine(sum == 0.3); // False
Console.WriteLine(sum);        // 0.30000000000000004

Example: decimal avoids that issue for financial-style values

decimal x = 0.1m;
decimal y = 0.2m;
decimal sum = x + y;

Console.WriteLine(sum == 0.3m); // True
Console.WriteLine(sum);         // 0.3

Important rule: do not mix them directly

This will not compile:

decimal amount = 10.5m;
double rate = 1.2;
var result = amount * rate;

You must explicitly choose one type and convert carefully if needed.

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:

  1. subtotal is created as a decimal with value 99.95.
  2. tax is created as a decimal with value 8.00.
  3. total is calculated by adding the two decimal values.
  4. The result is stored as 107.95.
  5. Console.WriteLine(total) prints 107.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:

  1. a is assigned an approximation of 0.1 in binary floating-point.
  2. b is assigned an approximation of 0.2 in binary floating-point.
  3. Adding them combines those approximations.
  4. The final stored result is very close to 0.3, but not exactly 0.3.
  5. Printing the value may show 0.30000000000000004.

The key lesson is that double is often approximately correct, while decimal is usually the better fit when exact decimal behavior matters.

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.

Take Quiz

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

Developers often avoid exact equality with double.

double x = 0.1 + 0.2;
double expected = 0.3;
double epsilon = 0.0000001;

bool areClose = Math.Abs(x - expected) < epsilon;

Practical team rule

A common codebase rule is:

  • Financial domain models use decimal
  • Scientific, geometry, or performance-sensitive calculations use double

This keeps code consistent and reduces bugs.

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 double values with ==

Broken example:

double result = 0.1 + 0.2;
Console.WriteLine(result == 0.3);

This may be False.

Better:

double result = 0.1 + 0.2;
double epsilon = 0.0000001;
Console.WriteLine(Math.Abs(result - 0.3) < epsilon);

4. Mixing decimal and double

Broken example:

decimal amount = 10.5m;
double multiplier = 2.0;
var total = amount * multiplier;

This does not compile because the types are different.

Avoid this by picking one type for the calculation.

5. Assuming bigger numbers require double

Beginners sometimes think large money values should use double. In most business applications, a value like 100000000.00 is still a normal decimal use case. The key question is not just size. It is whether decimal precision matters.

Comparisons

Featuredoubledecimal
Internal styleBinary floating-pointDecimal floating-point
Decimal precisionApproximate for many decimal fractionsBetter suited for exact decimal fractions
Typical useScience, graphics, measurementsMoney, finance, accounting
SpeedUsually fasterUsually slower
RangeWiderSmaller than double
Memory8 bytes16 bytes
Good for 0.1 + 0.2 == 0.3No, not reliablyYes, for decimal literals
Good for currencyUsually noYes

float vs double vs decimal

TypeBest forNotes
floatLower-precision scientific/graphics workLess precise than double
doubleGeneral floating-point mathFast and common for math-heavy code
decimalFinancial calculationsMore precise for base-10 values

Simple decision guide

  • Need exact decimal-style arithmetic for money? Use decimal.
  • Need fast floating-point math for measurements or simulations? Use double.
  • Need a smaller floating-point type and can accept less precision? Consider float.
Take Quiz

Cheat Sheet

Quick rules

  • Use decimal for money.
  • Use double for scientific and measurement-based calculations.
  • Use m suffix for decimal literals.
  • Do not compare double values with == unless you truly know it is safe.
  • Do not mix decimal and double in 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

decimal total = 0.1m + 0.2m;

Remember

  • double = approximate binary floating-point
  • decimal = decimal-oriented precision
  • Money = decimal

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 double and decimal?

Yes, but you should do it carefully because conversion may lose precision or require explicit casting.

Is double faster than decimal?

Yes, in many cases double is faster and more natural for CPU math operations. That is one reason it is preferred for scientific and graphics code.

Take Quiz

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

Take Quiz

Keep learning

Related questions

View more