Round a float DOWN to the nearest Integer in Python

avatar

Borislav Hadzhiev

Last updated: Jul 12, 2022

banner

Check out my new book

Round a float DOWN to the nearest Integer in Python #

Use the math.floor() method to round a float down to the nearest integer, e.g. result = math.floor(3.999). The math.floor method returns the largest integer less than or equal to the provided number.

main.py
import math # ✅ round float down to nearest integer using math.floor() result_1 = math.floor(3.999) print(result_1) # 👉️ 3 result_2 = math.floor(3.14) print(result_2) # 👉️ 3 result_3 = math.floor(-3.6) print(result_3) # 👉️ -4 # -------------------------------------------------- # ✅ round float down to nearest integer using int() result_4 = int(3.999) print(result_4) # 👉️ 3 result_5 = int(3.14) print(result_5) # 👉️ 3 result_6 = int(-3.6) print(result_6) # 👉️ -3

We used the math.floor() method to round a floating-point number down to the nearest integer.

main.py
import math print(math.floor(6.99)) # 👉️ 6 print(math.floor(6.01)) # 👉️ 6 print(math.floor(-6.01)) # 👉️ -7

The math.floor method returns the largest integer less than or equal to the provided number.

When passed a negative number, the math.floor() method rounds towards negative infinity.

This is different than the int() class, which rounds towards zero.

Round a float DOWN to the nearest Integer using int() #

Use the int() class to round a float down to the nearest integer, e.g. result = int(3.999). The int() class truncates floating-point numbers towards zero, so it will return an int that represents the number without the decimal places.

main.py
result_1 = int(3.999) print(result_1) # 👉️ 3 result_2 = int(3.14) print(result_2) # 👉️ 3 result_3 = int(-3.6) print(result_3) # 👉️ -3

The int class returns an integer object constructed from the provided number or string argument.

If passed a floating-point number, the int() class truncates towards zero.
main.py
print(int(-11.1)) # 👉️ -11 print(int(-13.9)) # 👉️ -13

Notice that there is a difference to how math.floor() and int() handle negative numbers:

  • math.floor() rounds towards negative infinity
  • int() truncates towards zero

Alternatively, you can use the math.trunc() method.

Round a float DOWN to the nearest Integer using math.trunc() #

Use the math.trunc() method to round a float down to the nearest integer, e.g. result = math.trunc(3.999). The math.trunc() method removes the fractional part and returns the integer part of the given number.

main.py
import math result_1 = math.trunc(3.999) print(result_1) # 👉️ 3 result_2 = math.trunc(3.14) print(result_2) # 👉️ 3 result_3 = math.trunc(-3.6) print(result_3) # 👉️ -3

The math.trunc method takes a number, removes its fractional part and returns its integer part.

The math.trunc() method rounds towards zero.

main.py
import math print(math.trunc(-9.1)) # 👉️ -9 print(math.trunc(-9.9)) # 👉️ -9

This approach achieves the same result as passing the floating-point number to the int() class.

Use the search field on my Home Page to filter through my more than 3,000 articles.