Jan-15-2020, 07:11 AM
It's an issue caused by floating point arithmetic
look also at https://docs.python.org/3/tutorial/floatingpoint.html
In real life, when dealing with number representing money amounts one would make sure to always work with correct precision, so that there is no artefacts like extra cent due to rounding accumulated over time. One way is to use decimal module
look also at https://docs.python.org/3/tutorial/floatingpoint.html
>>> deposit = 1000.2 >>> withdrawal = 1000.1 >>> balance = deposit - withdrawal >>> balance 0.10000000000002274 >>> print('{:.2f}'.format(balance)) 0.10 >>> print(f'{balance:.2f}') 0.10 >>>one way to deal with it (sufficient at least for homework purposes) is to use string formatting - f-strings or str.format() method so that you represent the result, being float, up to 2 decimal places precision
In real life, when dealing with number representing money amounts one would make sure to always work with correct precision, so that there is no artefacts like extra cent due to rounding accumulated over time. One way is to use decimal module
>>> from decimal import Decimal >>> deposit = Decimal('1000.2') >>> withdrawal = Decimal('1000.1') >>> balance = deposit - withdrawal >>> balance Decimal('0.1') >>> print(balance) 0.1
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs