int(7.5)
and floor(7.5)
both return 7
because the former truncates decimals and the latter rounds them down. Is there any difference between the two?
Asked
Active
Viewed 103 times
0

Alec
- 8,529
- 8
- 37
- 63
-
it has been explained in details here https://stackoverflow.com/questions/31036098/what-is-the-difference-between-int-and-floor-in-python-3 – lilgallon Apr 27 '19 at 19:30
-
Note also that `round(7.5)` and `round(8.5)` both return 8. But the documentation is quite clear on these things; read it. – o11c Apr 27 '19 at 19:32
1 Answers
3
For positive numbers, truncating at the decimal point and rounding down have the same effect. For negative numbers though, int()
returns a number one greater than floor()
int(-7.5) == -7 # True
floor(-7.5) == -8 # True

Alec
- 8,529
- 8
- 37
- 63