I noticed that when I type int(0.9) it becomes 0 instead of 1.
I was just wondering why Python wouldn't round the numbers.
I noticed that when I type int(0.9) it becomes 0 instead of 1.
I was just wondering why Python wouldn't round the numbers.
int does not perform rounding in the way you expect, it rounds to 0, the round function will round to the nearest whole number or places you provide.
>>> int(0.9)
0
>>> int(-0.9)
0
>>> round(12.57)
13
>>> round(12.57, 1)
12.6
If you use int function, it ignores decimal points and gives you integer part. So use round function if you want to round figures.
If x is floating point, the conversion truncates towards zero.
Source: docs.
Simple: because int() works as "floor" , it simply cuts of instead of doing rounding. Or as "ceil" for negative values.
You want to use the round() function instead.