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:

  1. This returns a float
  2. This rounds towards -∞

Leave a Reply

Your email address will not be published. Required fields are marked *