Search code examples
pythonpython-3.xmathfloor

Taking the floor of a float


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?


Solution

  • 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.