python – Round a floating-point number down to the nearest integer?
python – Round a floating-point number down to the nearest integer?
Simple
print int(x)
will work as well.
One of these should work:
import math
math.trunc(1.5)
> 1
math.trunc(-1.5)
> -1
math.floor(1.5)
> 1
math.floor(-1.5)
> -2
python – Round a floating-point number down to the nearest integer?
x//1
The //
operator returns the floor of the division. Since dividing by 1 doesnt change your number, this is equivalent to floor but no import is needed.
Notes:
- This returns a float
- This rounds towards -∞