The round function in Python

In order to round the given number to the nearest integer or required precision (number of digits after decimal point), you may use the Python round() function.

round() float example round list of floats example

Syntax of using round function

The round() function in Python takes two parameters:

round(number[, ndigits]

  • The round() function returns the rounded value that is closest to the integer to its given value if ndigits is not provided.
  • The ndigits specifies the precision after the decimal point.

See the examples below for learning how round Python function works with positive, negative, zero and without providing the ndigits parameter.

An example of round function with float value and no precision parameter

In this example, a float value is passed to the round function. No other value is given i.e. ndigits parameter. Have a look at the code and output:

See online demo and code


round float

You can see it returned 11. As such, the rule for rounding the number is any number greater than or equal to 5 is rounded to the next whole number. That means if the number to be rounded is 5.5 then after rounding its value will be 6.

If the number is 4,3,2, 1 or 0, it remains the same. So, for rounding 5.4 will return 5. See the demonstration below by using the round function along with the second parameter.

The example of rounding with ndigit parameter

In the following example, the round() function is used with both parameters. A float number is passed with 4 numbers after the decimal point. The ndigit parameter is given the 2 value so round() will return two digits – see how it worked:

See online demo and code


Python round

The returned result is 25.54 as next digit is less than 5. What if it was 5 or more, see next example.

See online demo and code

round precision

Note: You may see some surprising results as rounding the numbers while using the round function. For example, round(5.675)) yields 5.67 while it should be 5.68. This is explained in the official documentation of Python website here.

Rounding a float list example

In the following example, a list of float items is created. This is followed by using the for loop to iterate through the list items and executing the round function on each item. The rounded items are added to a new list and displayed, have a look:

See online demo and code

round list

Using precision in the round function for list

And of course, you may apply the precision in above example for the required rounding of numbers. Using almost the same code as in above example with the addition to using the other parameter (specifying precision):

See online demo and code

round list precision

More practical example – using round function for Fahrenheit to Celsius conversion

As converting Fahrenheit to Celsius in Python programs, it returns really big numbers with a decimal point. The round function can really be useful in that scenario. I have shown Fahrenheit to Celsius conversion in other tutorials (map, lambda etc.) and converted numbers are like 36.66666666666667, 38.88888888888889.

In this example, the round function is used in the lambda function for getting the rounded temperatures. A list of Fahrenheit temperatures is created and lambda function is used for converting it to Celsius. The returned list after conversion is displayed with and without round function:

See online demo and code

round F-to-C

You can see, the precision value is given as 1.