+ 1
Why is 2.3+3.4=5.69999999?
print(float(“2.3”)+float(“3.4”)) The result comes out to be 5.699999 Shouldn’t it be 5.7?
3 Antworten
+ 13
That is because I believe in python (and not only) float numbers are approximated. You can find plenty of situations where this happens if you play with floats
+ 10
Because of the imprecision of float in Python
It will work better if you use decimal.Decimal
+ 2
you could also try adding this to your code:
print(round(float(“2.3”)+float(“3.4”), 5))
round with 5 as the second argument will round the value so that only 5 decimal places are displaying. And in cases like this where you are extremely close to the intended answer, you will actually get the intended answer (in this case 5.7).
Just remember that if the answer would be a long ugly decimal - for example float(10/7) - you will only see 5 decimals of the answer.