In this tutorial, I will explain how to use the round() function in Python. As a Python developer working on a project for one of my USA clients, I came across a scenario where I needed to round numbers in my code. I explored more about the round() function and I will share my findings in this article with examples.
Syntax of round() Function in Python
The round() function in Python takes two parameters: the number you want to round and, optionally, the number of decimal places you want to round to. The basic syntax looks like this:
round(number, ndigits)Here, number is the numeric value you want to round, and ndigits is an optional parameter specifying the number of decimal places to round to. If ndigits is not provided, the round() function will round the number to the nearest integer.
Examples
Let us understand more about the round() function by working on a few examples.
Read How to Use the Floor() Function in Python?
1. Round Numbers to Decimal Places
Let’s say you’re working on a project for a company based in New York, and you need to round some sales figures to two decimal places. Here’s how you can use the round() function to achieve this:
sales_figure = 1234.5678
rounded_figure = round(sales_figure, 2)
print(rounded_figure) Output:
1234.57I have executed the above example and added the screenshot below.

In this example, we have a sales_figure of 1234.5678. By passing this number to the round() function along with ndigits=2 , we round the figure to two decimal places, resulting in 1234.57.
Check out How to Use Built-In Functions in Python?
2. Round Numbers to the Nearest Integer
If you need to round a number to the nearest integer, you can simply omit the ndigits parameter when calling the round() function. Here’s an example:
population = 8675309.5
rounded_population = round(population)
print(rounded_population) Output:
8675310I have executed the above example and added the screenshot below.

In this case, we have a population value of 8675309.5. By calling round(population) without specifying ndigits , the function rounds the number to the nearest integer, giving us 8675310.
Read How to Use the find() Function in Python?
3. Round Numbers with Decimal Places and Negative ndigits
In addition to rounding numbers to a specific number of decimal places, you can also use the round() function to round numbers to the nearest 10, 100, 1000, and so on. To do this, you can pass a negative value for the ndigits parameter. Here’s an example:
us_population = 331002651
rounded_us_population = round(us_population, -6)
print(rounded_us_population) Output:
331000000I have executed the above example and added the screenshot below.

In this example, we have the us_population set to 331002651. By calling round(us_population, -6) , we round the number to the nearest million, resulting in 331000000.
Check out How to Use Exponential Functions in Python?
4. Round Floating-Point Numbers
When working with floating-point numbers in Python, it’s important to be aware of potential precision issues. Due to the way floating-point numbers are represented internally, rounding them may sometimes lead to unexpected results. Here’s an example:
pi = 3.14159
rounded_pi = round(pi, 3)
print(rounded_pi) Output:
3.142In this case, we have the value of pi stored in the pi variable. When we round it to three decimal places using round(pi, 3), the result is 3.142 instead of the expected 3.141. This is due to the limitations of floating-point arithmetic in Python.
To mitigate this issue, you can use the decimal module in Python, which provides support for decimal floating-point arithmetic. Here’s an example:
from decimal import Decimal
price = Decimal('10.4567')
rounded_price = round(price, 2)
print(rounded_price) Output:
10.46By using the Decimal class from the decimal module, you can achieve more precise rounding results when working with floating-point numbers.
Read How to Use Counter Function in Python?
Handle Negative Numbers
The round() function works seamlessly with negative numbers as well. When rounding negative numbers, it’s important to keep in mind that the function rounds away from zero. Here’s an example to illustrate this:
temperature = -3.7
rounded_temperature = round(temperature)
print(rounded_temperature) Output:
-4In this scenario, we have a temperature of -3.7. When we round this number using round(temperature) , the result is -4 because the function rounds away from zero for negative numbers.
Check out How to Create a Square Function in Python?
Conclusion
In this tutorial, I helped you to learn how to use the round() function in Python. I covered syntax of the round() function in Python and some examples like rounding numbers in decimal places , rounding numbers to the nearest integer , rounding numbers with decimal places and negative ndigits , and how to round floating point numbers. We also discussed how to handle negative numbers.
You may also like to read:
- What is Python Return Statement?
- How to Use the Mean() Function in Python?
- Python Class Method vs Static Method

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.