Taking the floor of a float
Solution 1
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.
Solution 2
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 floor
f1 = 3.1415
f2 = 3.7415
print floor(f1) # 3.0
print int(floor(f1)) # 3
print int(f1) # 3
print int(f2) # 3 (some people may expect 4 here)
print int(floor(f2)) # 3
http://docs.python.org/library/functions.html#int
Solution 3
The second approach is the way to go, but there's a way to shorten it.
from math import floor
floor(3.1415)
Solution 4
Beware that taking the floor and casting to an int are not the same thing with negative numbers. If you really want the floor as an integer, you should cast to an int after calling math.floor().
>>> int(-0.5)
0
>>> math.floor(-0.5)
-1.0
>>> int(math.floor(-0.5))
-1
Solution 5
Cast it to int
if you don't want a float
int(3.1415 // 1)
Related videos on Youtube
Randomblue
Updated on July 09, 2022Comments
-
Randomblue almost 2 years
I have found two ways of taking floors in Python:
3.1415 // 1
and
import math math.floor(3.1415)
The problem with the first approach is that it return a float (namely
3.0
). The second approach feels clumsy and too long.Are there alternative solutions for taking floors in Python?
-
Karoly Horvath over 12 yearspick the second one. nothing wrong with it.
-
Thomas Orozco over 12 yearsYou could always call
int(3.1415)
, but rather than a floor, it rounds towards 0, which will yield a different result for numbers <0. -
wim over 12 years
-
Stam Kaly almost 5 yearsor you could do
int(3.1415 // 1)
which works like math.floor
-
-
Randomblue over 12 yearsWhy would people expect
4
forint(f2)
? -
Matt Alcock over 12 yearsBecause you may expect it to round to the nearest int not the lower int.
-
Sven Marnach over 12 yearsThe OP is using Python 3.x, which isn't quite obvious from the post. In Python 3.x,
math.floor()
returns anint
, so it's not necessary to convert the return value. -
Eugene Yarmash about 11 years
floor()
returns an integer in python3, but in python2 it returns float. -
Mark Ransom about 11 years@eugeney, I was just pointing out that there was a way to leave off the
math.
part. I wonder why they didn't complain about getting afloat
result back fromfloor
in the first place? And thanks for informing me of that change from 2 to 3, I didn't know that one. It makes sense,floor
couldn't return anint
until arbitrary size integers were interchangeable with normal ints starting in 2.4. -
zezollo almost 7 yearsTo get any number (positive as well as negative) to round down, there's still
int(n // 1)
, likeint(-3.14 // 1)
that gives-4
. Of course this is only useful if negative numbers might show up. -
Sven Marnach almost 7 years@zezollo Unless you want to confuse readers of your code, using
int(math.floor(x))
is preferable over floor division by 1, since it's immediately obvious what it does.