Python’s round() function lets you round numbers to the nearest whole number or to a specific number of decimal places. The function has two parameters and is built into the programming language.

What is Python round() and what is it used for?

If you want to round numbers in Python, the built-in round() function is the simplest and most effective method for doing so. This function returns a floating-point number that is rounded to the nearest value based on the number of decimal places you specify. By default, however, the programming language rounds to zero decimal places when implementing this function, returning the nearest integer. In the following sections, we’ll look at how to round numbers in Python, both with and without decimal places.

Managed Nextcloud from IONOS Cloud
Work together in your own cloud
  • Industry-leading security
  • Communication and collaboration tools
  • Hosted and developed in Europe

What is the syntax for Python round()?

Before we get to the examples, let’s first take a look at the syntax for Python round(). Here’s how the function is structured:

round(number, decimal place)
python

The first parameter is the number that should be rounded. While the first parameter is mandatory, the second parameter is optional. In the second parameter, you can specify the number of decimal places that the number should be rounded to. The default is 0, so if you don’t enter a number for this parameter, the number you provide in the first parameter will be rounded to the nearest whole number. However, if you, for example, enter the number 2 for this parameter, the output will be rounded to two decimal places.

Example of Python round() without decimal places

In the following example, we’re going to use the Python round() function without specifying a number for the second parameter. First, we’re going to define the number we want to round, then we’ll run the function. Since the function is included when you install Python, you don’t need to install or import anything else. Here’s what the code looks like:

number = 3.7835738292
rounded_number = round(number)
print(rounded_number)
python

When you run this code, you’ll receive the following output:

4
python

Alternatively, you can pass the number you want to round directly in round() instead of creating a separate variable for the number. If you use this approach, here’s what the code will look like:

rounded_number = round(3.7835738292)
print(rounded_number)
python

The code here gives you the same result as the code above: 4.

Example of Python round() with decimal places

If you use the parameter for decimal places, you can get a more precise results when working with Python round(). We’re going to take the number from the example above and instruct the system to round it to two decimal places. Here’s what the code looks like:

number = 3.7835738292
rounded_number = round(number, 2)
print(rounded_number)
python

Just as in the example above, we can also pass the number directly as an argument in Python’s round function instead of first creating a variable named ‘number’:

rounded_number = round(3.7835738292, 2)
print(rounded_number)
python

It doesn’t matter which approach you choose; both produce the same result:

3.78
python
Tip

Deploy websites and apps directly from GitHub with Deploy Now from IONOS. Enjoy automated deployments, a quick setup and scalable performance. Simply choose the project type that fits your requirements and goals!

Was this article helpful?
Go to Main Menu