Taking the floor of a float Taking the floor of a float python-3.x python-3.x

Taking the floor of a float


As long as your numbers are positive, you can simply convert to an int to round down to the next integer:

>>> int(3.1415)3

For negative integers, this will round up, though.


You can call int() on the float to cast to the lower int (not obviously the floor but more elegant)

int(3.745)  #3

Alternatively call int on the floor result.

from math import floorf1 = 3.1415f2 = 3.7415print floor(f1)       # 3.0print int(floor(f1))  # 3print int(f1)         # 3print int(f2)         # 3 (some people may expect 4 here)print int(floor(f2))  # 3

http://docs.python.org/library/functions.html#int


The second approach is the way to go, but there's a way to shorten it.

from math import floorfloor(3.1415)