Search code examples
pythonrounding

How to round down a float using import math


I have a variable that is a float, and I can't find out how to round it down.

I did a google search and it said I should use trunc, but trunc didn't work for me.


Solution

  • if you always want to round down and are getting positive floats, just use int() .. otherwise you can use math.floor(), which can handle negative floats too

    >>> math.floor(3.99)
    3
    >>> int(-0.1)
    0
    >>> int(-1.2)
    -1
    >>> math.floor(-1.2)
    -2