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
  • 137,827
  • 25
  • 176
  • 248

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 is tired
  • 6,860
  • 20
  • 39
  • 51
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,419
  • 17
  • 13
2

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

Source: docs.

Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
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
  • 137,827
  • 25
  • 176
  • 248