Why 0.1 + 0.2 is not equal to 0.3 in C# (and in most languages)

Learn why 0.1 + 0.2 ? 0.3 in C# and most modern programming languages. Discover how to handle floating-point arithmetic and best practices for comparing numeric values. Q2BSTUDIO offers custom solutions in software development, artificial intelligence, cyberse

sábado, 16 de agosto de 2025 • 2 min read • Q2BSTUDIO Team

Artificial-Intelligence-

This article was originally published on Hashnode and here you have a Spanish version that explains why 0.1 + 0.2 != 0.3 in C# and in most other languages

Many developers, even experienced ones, are surprised to see that in C# the expression Console.WriteLine(0.1 + 0.2 == 0.3) prints False

Why does this happen? The reason lies in how floating-point numbers are represented in binary. C# and most modern languages follow the IEEE 754 standard for float and double. Decimal numbers like 0.1 and 0.2 cannot be represented exactly in binary, so the sum is not exactly 0.3 but a close value, for example 0.30000000000000004

That small representation error makes 0.30000000000000004 != 0.3 and that is why the comparison with == returns False. This behavior is not exclusive to C#. In Python print(0.1 + 0.2 == 0.3) gives False. In JavaScript console.log(0.1 + 0.2 === 0.3) also gives False. In Java the comparison 0.1 + 0.2 == 0.3 returns False. They all use binary floating-point arithmetic and share the same limitation

What to do instead of using == directly? A good practice is to compare with an epsilon tolerance. For example, a function AreEqual(a, b, epsilon = 1e-10) can return abs(a - b) < epsilon. This technique is the recommended one for comparing floating-point values in most languages

In C#, when decimal precision is needed, such as in financial calculations, the decimal type should be used, which uses base 10 and avoids the rounding problems intrinsic to binary representation. For example decimal a = 0.1m; decimal b = 0.2m; then a + b == 0.3m will be True

Summary of good practices: do not compare floating-point numbers with ==; use an appropriate epsilon for comparisons; use decimal for monetary applications or when exact precision is required; remember that this behavior comes from the IEEE 754 standard and appears in many languages

At Q2BSTUDIO we are a custom software and application development company specialized in modern solutions. We offer custom software, custom application development, artificial intelligence implementations, cybersecurity, and AWS and Azure cloud services. We help companies adopt business intelligence services and AI for businesses, create AI agents, and solutions with Power BI to improve data visualization and analysis

Our team combines experience in custom software and custom applications with advanced knowledge in artificial intelligence and cybersecurity to deliver secure and scalable projects. We also provide AWS and Azure cloud services to deploy and maintain cloud infrastructures, and business intelligence services to transform data into decisions

If you work with numbers and models that require precision, keep in mind the limitations of floating-point arithmetic and apply the appropriate techniques described here. At Q2BSTUDIO we can advise you on choosing numeric types, artificial intelligence implementations, AI agents, and Power BI integrations so that your custom applications are accurate and reliable

Relevant keywords: custom applications, custom software, artificial intelligence, cybersecurity, AWS and Azure cloud services, business intelligence services, AI for businesses, AI agents, Power BI

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.