{
  "id": 18320184,
  "title": "Why Does 0.1 + 0.2 Not Equal 0.3 in Python?",
  "url": "https://urgent.news/2026/11/05/why-does-0-1-0-2-not-equal-0-3-in-python",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-05T14:37:31.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/cstackacademy/why-does-01-02-not-equal-03-in-python-14k"
  },
  "original_language": "en",
  "account": "When you type `print(0.1 + 0.2)` in Python, you expect the output `0.3`. However, Python actually displays `0.30000000000000004`. This phenomenon is not unique to Python, but occurs in many other programming languages as well. The reason behind this discrepancy lies in how computers store and calculate numbers.\n\nComputers operate using a binary system, which means they represent numbers in base 2. While humans typically count in base 10 (decimal), where each digit represents a power of 10 (ones, tens, hundreds, etc.), computers can only work with halves, quarters, eighths, sixteenths, and so on. While some numbers, like 0.5 (one half) and 0.25 (one quarter), can be precisely represented in binary, others like 0.1 (one tenth) cannot.\n\nThe decimal number 0.1 is represented in binary as an endless repeating pattern: `0.0001100110011001100110011...`. Computers have a fixed amount of space, usually 64 bits, to store each number. To fit the binary representation of 0.1 within this constraint, the computer stores the closest value it can, which is slightly more than 0.1.\n\nWhen you add 0.1 and 0.2 in Python, the resulting value is even slightly more than 0.3. The computer stores this slightly-off value, and when it is printed, Python displays the shortest number that maps back to the stored value. In this case, it is `0.30000000000000004`.\n\nThis same issue arises in other programming languages, such as JavaScript, Java, and C#. The same principles apply, regardless of the language you are using.\n\nTo address this problem, there are a few strategies you can employ:\n\n1. Round when displaying a result: If you only need to show the number, round it to the desired number of decimal places. For example, `round(total, 2)` will display `0.3` instead of `0.30000000000000004`.\n\n2. Never compare floats using the `==` operator: Instead of checking for exact equality, use a tolerance level to determine if two floating-point numbers are close enough to be considered equal. For instance, `math.isclose(0.1 + 0.2, 0.3)` returns `True`, indicating that the two numbers are close enough to be deemed equal.\n\n3. Use the `Decimal` module for precise decimal calculations: When working with money or other situations where exact decimal arithmetic is crucial, use the `decimal` module in Python. Pass the numbers as strings, like `Decimal('0.1') + Decimal('0.2')`, which will yield the expected result of `0.3`.\n\nUnderstanding the limitations of floating-point arithmetic and adopting these best practices can help avoid unexpected results when working with decimal numbers in programming.",
  "summary": "Open Python and type this: print ( 0.1 + 0.2 ) You'd expect 0.3 . Python gives you this: 0.30000000000000004 And it gets stranger: print ( 0.1 + 0.2 == 0.3 ) False If you're learning to code, this is the moment you start wondering whether you broke something. You didn't. Python is doing exactly what it was designed to do, and so are JavaScript, Java, C# and almost every other language. Try 0.1 +…",
  "key_points": [
    "Python displays 0.30000000000000004 instead of 0.3 when adding 0.1 and 0.2",
    "Computers use binary system, unable to precisely represent decimal numbers like 0.1",
    "Strategies to handle floating-point issues: rounding, tolerance comparison, using Decimal module"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}