Skip to content

Python round() Function

  • by

Python round() Function is used to get a number rounded to the specified number of decimals. This method rounds off the digits of a number and returns the floating-point number.

round(number, digits) 

Note: The default number of decimals is 0

Python round() Function

Simple example code round number if the second parameter is missing.

# for integers
print("int", round(10))

# for floating point
print("flot", round(10.7))

# even choice
print("even", round(5.5))

Output:

Python round() Function

Round a number to the given number of decimal places

    print(round(2.665, 2))  
    # cannot be represented exactly as a float  
    print(round(2.675, 2))  

Output:

2.67
2.67

It’s important to be aware that round() uses the “round half to even” strategy, also known as “bankers’ rounding”, which means that when rounding to a specific number of digits, if the number is exactly halfway between two possible rounded values, it rounds to the nearest even number. For example:

print(round(2.5))  # Output: 2
print(round(3.5))  # Output: 4

Do comment if you have any doubts or suggestions on this Python function 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