Taking the floor of a float

Randomblue picture Randomblue · Feb 23, 2012 · Viewed 86k times · Source

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?

Answer

Sven Marnach picture Sven Marnach · Feb 23, 2012

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.