I am trying to add floats to get 9.9, anyway when I run this code: 3.3 + 3.3 + 3.3, The end result is 9.899999999999999 Is there a way to fix this?
Asked
Active
Viewed 48 times
3
-
Don't worry about it, that's just an artifact of how floating-point arithmetic works. The way to "fix" it is to format your numbers correctly when you print them. – PM 2Ring May 01 '18 at 10:38
-
How would i go about forming them correctly? – L.And May 01 '18 at 10:40
-
@L.And, go through the answers of the questions marked as duplicates. You'll find many useful answers to your question *How would i go about forming them correctly?* – Keyur Potdar May 01 '18 at 10:41
-
As Keyur said, there are various options, depending on what you want. If you only want one digit after the decimal point you can do `n = 3.3 + 3.3 + 3.3; print('{:.1f}'.format(n))` – PM 2Ring May 01 '18 at 10:44