1

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.

GhostCat
  • 133,361
  • 24
  • 165
  • 234

4 Answers4

3

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
Nick stands with Ukraine
  • 6,365
  • 19
  • 41
  • 49
2

If you use int function, it ignores decimal points and gives you integer part. So use round function if you want to round figures.

Jay Parikh
  • 2,243
  • 16
  • 13
2

If x is floating point, the conversion truncates towards zero.

Source: docs.

Brad Solomon
  • 34,372
  • 28
  • 129
  • 206
2

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.

GhostCat
  • 133,361
  • 24
  • 165
  • 234