Skip to content

Python round down

  • by

Use the math.floor() Method to Round Down a Number to the Nearest Whole Number Python. Round Down reduces a number to the nearest lower integer.

math.floor(number)

This method returns the nearest integer that is less than or equal to a given number.

Python round-down example

Simple example code first imports the math module. Then pass the number to be rounded down to the math.floor() method:

from math import floor

print(floor(5.1))

print(floor(7.9))

print((-0.6))

Output:

Python round down

Using string format

def roundDown(n):
    return int("{:.0f}".format(n))

roundedValue = roundDown(2.5)

Using int() method

# Round down number using int() method
number = 10.5
print(int(number))
number = -4.6
print(int(number))

Using in-built round() method

number = 12.345
# Here, by default, decimal_places = 0
print(round(number))  

Using trunc() method

from math import trunc

number = 5.25
print(trunc(number))
number = 7.89
print(trunc(number))

Do comment if you have any doubts or suggestions on this Python round topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading