+ 2

print(int(2.9999999999999998)) Python 3

I noticed something as I tried printing a float as an integer. If you tried to print a float with 16 decimal places consisting of .9999999999999997 or any value below, the output will not round the number Example: print(int(2.9999999999999997)) output: 2 But if we go up to .9999999999999998 it rounds the number Example: print(int(2.9999999999999998)) output: 3 Does anyone know why this is the case? I am new to Python 3 and programming in general, so any help would be greatly appreciated.

15th Feb 2019, 4:04 AM
Ein
Ein - avatar
2 Answers
+ 2
Decimal is only the presentation. The computer stores number in binary. Therefore, it stores 2.9999....8 as 3.000000 If you want to know the details, Anna explains very well in this Q&A : https://www.sololearn.com/Discuss/1477626/?ref=app
15th Feb 2019, 5:43 AM
Gordon
Gordon - avatar
+ 2
https://docs.python.org/2/tutorial/floatingpoint.html
15th Feb 2019, 2:48 PM
Shuaib Nuruddin
Shuaib Nuruddin - avatar