Inaccuracy of decimal in .NET
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
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:
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.
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.
The safer pattern is to use decimal literals directly when the value is meant to be decimal:
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
decimalto represent repeating decimals such as1/3exactly is unrealistic because the type still has finite precision. - Mixing
doubleanddecimaloften smuggles floating-point issues into code that was meant to avoid them. - Forgetting to use the
msuffix on decimal literals causes values to be treated asdoublefirst. - Treating rounding policy as an afterthought can create business-level inaccuracies even when the arithmetic type is appropriate.
- Using
decimaleverywhere without considering performance or domain needs can slow code unnecessarily.
Summary
- '
.NET decimalis 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
mliterals and avoiding unnecessary conversion fromdouble. - Choose rounding rules explicitly, especially in financial code.
Related reading
- Index of Currently Selected Row in DataGridView
- Index Of Longest Run C
- Inheriting XML comments from interfaces in C
- Initializing IEnumerablestring In C
- Inline list initialization in VB.NET
- Inno Setup Verify that .NET 4.0 is installed
- INotifyPropertyChanged and Auto-Properties
- Insert data using Entity Framework model

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.