.NET
decimal inaccuracy
software development
programming
floating point precision

Inaccuracy of decimal in .NET

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In .NET, decimal is much better than double for money and other base-10 values, but it is not magically perfect. It avoids many binary floating-point surprises, yet it still has finite precision, rounding rules, and arithmetic cases where results are not exact because the value simply cannot be represented with a limited number of decimal digits.

What decimal Actually Improves

The main reason developers choose decimal is that it stores decimal fractions in a decimal-oriented format instead of a binary floating-point format. That means values such as 0.1m and 0.2m are represented much more naturally than they are with double.

csharp
1decimal a = 0.1m;
2decimal b = 0.2m;
3decimal c = a + b;
4
5Console.WriteLine(c); // 0.3

That is one of the big advantages of decimal in financial software.

Why It Still Is Not Infinite Precision

decimal has finite precision, roughly 28 to 29 significant digits. So while it is exact for many common currency-style values, it still cannot represent every possible decimal result forever.

Division is the easiest example:

csharp
decimal x = 1m / 3m;
Console.WriteLine(x);

The result is rounded because 1/3 has an infinite repeating decimal expansion. The type is accurate within its precision limits, but it cannot store infinitely many digits.

So when developers talk about decimal being "inaccurate," the real issue is often that they expected infinite precision rather than high finite precision.

Rounding Is Part of the Story

Another source of surprise is that financial logic often requires explicit rounding rules. Even when decimal stores a value exactly, later business rules may require rounding to two decimal places, bankers' rounding, or some other policy.

csharp
1decimal value = 10m / 6m;
2decimal rounded = Math.Round(value, 2, MidpointRounding.ToEven);
3
4Console.WriteLine(value);
5Console.WriteLine(rounded);

If the rounding mode is not chosen deliberately, the application can produce results that are technically valid but still wrong for the business domain.

Conversions Can Reintroduce Problems

A very common mistake is mixing decimal with double. If a value starts as a binary floating-point number, converting it later to decimal does not undo the earlier representation issue.

csharp
1double raw = 0.1;
2decimal converted = (decimal)raw;
3
4Console.WriteLine(converted);

The safer pattern is to use decimal literals directly when the value is meant to be decimal:

csharp
decimal amount = 0.1m;

That keeps the computation in the decimal domain from the start.

Equality and Comparison Need Care Too

Because decimal is precise within its rules, equality checks are usually safer than with double, but you still need to think about whether you are comparing raw arithmetic results or already-rounded business values.

For example, these two expressions may be mathematically close but not equal until rounded to the same scale. In finance code, that often means comparing normalized values rather than raw intermediates.

When decimal Is the Right Tool

Use decimal when:

  • the values are base-10 quantities such as money, rates, or tax amounts
  • exact representation of common decimal fractions matters
  • the code should avoid binary floating-point surprises

Do not use it just because it sounds "more accurate" in every context. For many scientific or high-throughput numeric workloads, double is faster and often the correct engineering choice.

Common Pitfalls

  • Expecting decimal to represent repeating decimals such as 1/3 exactly is unrealistic because the type still has finite precision.
  • Mixing double and decimal often smuggles floating-point issues into code that was meant to avoid them.
  • Forgetting to use the m suffix on decimal literals causes values to be treated as double first.
  • Treating rounding policy as an afterthought can create business-level inaccuracies even when the arithmetic type is appropriate.
  • Using decimal everywhere without considering performance or domain needs can slow code unnecessarily.

Summary

  • '.NET decimal is designed to represent many decimal fractions more accurately than binary floating-point types.'
  • It is still a finite-precision numeric type, not infinite-precision math.
  • Division and some other operations may still require rounding because the exact result cannot be stored fully.
  • Keep values in the decimal domain from the beginning by using m literals and avoiding unnecessary conversion from double.
  • Choose rounding rules explicitly, especially in financial code.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.