Last modified: Feb 05, 2026 By Alexander Williams

Python Round Function Guide: Usage & Examples

Numbers are everywhere in programming. You often need to round them. Python provides a built-in tool for this. It is the round() function.

This guide explains how to use it. We will cover its syntax and behavior. You will see many examples. By the end, you will round numbers with confidence.

What is the Round Function?

The round() function returns a floating-point number. It is rounded to a specified number of digits. It is a built-in function. You do not need to import any module.

Its primary purpose is to simplify numbers. It makes them easier to read and use. It is crucial for financial calculations and data presentation. Understanding its rounding rules is key.

Basic Syntax and Parameters

The function's syntax is simple. It follows standard Python function syntax rules.


# Basic Syntax
round(number, ndigits)
    

It takes two arguments. The first is the number you want to round. The second is ndigits. It specifies the number of decimal places.

The ndigits argument is optional. If you omit it, the function rounds to the nearest integer. It returns an integer in that case.

Understanding these function parts and calls is the first step to mastery.

How Rounding Works: The "Round Half to Even" Rule

Python's rounding follows a specific rule. It is called "round half to even" or "bankers' rounding". It aims to reduce bias.

When a number is exactly halfway between two possibilities, it rounds to the nearest even number. This can be surprising at first.


# Examples of Bankers' Rounding
print(round(2.5))   # Number is halfway between 2 and 3
print(round(3.5))   # Number is halfway between 3 and 4
    

2
4
    

2.5 rounds down to 2 (the even number). 3.5 rounds up to 4 (the even number). This is different from the "round half up" method taught in school.

For numbers not exactly at the midpoint, it works as you expect. It rounds to the closest value.

Practical Examples of the Round Function

Let's look at practical code. These examples show the function in action.

Rounding to Integers

Omit the ndigits parameter. The function returns an integer.


# Rounding to the nearest integer
print(round(10.2))   # Output: 10
print(round(10.8))   # Output: 11
print(round(-5.7))   # Output: -6
    

Rounding to Decimal Places

Use a positive integer for ndigits. It rounds to that many decimal places.


# Rounding to 1 and 2 decimal places
pi = 3.14159265
print(round(pi, 1))  # Output: 3.1
print(round(pi, 4))  # Output: 3.1416

price = 19.987
print(round(price, 2)) # Output: 19.99
    

Rounding to Tens, Hundreds, etc.

Use a negative integer for ndigits. It rounds to the left of the decimal point.


# Rounding to nearest ten, hundred, thousand
population = 1234567
print(round(population, -1))  # To nearest ten: 1234570
print(round(population, -2))  # To nearest hundred: 1234600
print(round(population, -3))  # To nearest thousand: 1235000
    

Common Pitfalls and Important Notes

The round() function has some nuances. Be aware of them to avoid bugs.

Floating-Point Precision: Computers store floats imprecisely. A number like 2.675 might actually be 2.6749999999999998 in memory. This affects rounding.


# Floating-point precision issue
print(round(2.675, 2)) # Might output 2.67, not 2.68
    

For critical applications like finance, use the decimal module. It offers more precise control.

Return Type: The function returns an integer if ndigits is omitted or None. Otherwise, it returns a float. This is true even if the result looks like an integer.


print(type(round(5.6)))    # 
print(type(round(5.6, 0))) #  (value is 6.0)
    

Integrating Round with Other Functions

The round() function is often used with other operations. You can use techniques like function argument unpacking to pass values. It is also common in loops and data processing.

For instance, you might round results from a function that runs on a schedule. You can learn more about scheduling tasks in our guide on how to run Python functions every minute and 30 minutes.


# Example: Rounding a list of calculated values
results = [10.456, 20.789, 30.111]
rounded_results = [round(x, 1) for x in results]
print(rounded_results) # Output: [10.5, 20.8, 30.1]
    

Conclusion

The Python round() function is a versatile tool. It helps you manage numerical precision. Remember its "bankers' rounding" behavior.

Use positive ndigits for decimals. Use negative ndigits for large numbers. Be cautious with floating-point precision for exact results.

Practice with the examples provided. You will quickly master this essential function. It will make your data cleaner and your calculations more presentable.