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.

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:

#The round() function example with flaot



print("The round value for 10.87 = ",round(10.87))

Output:

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:

Code:

#The round() function with number and ndigit parameters


print("The round value for 25.5433 = ",round(25.5433,2))

Output:

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.

Code:

#The round() function with number and ndigit parameters



print("The round value for 25.5453 = ",round(25.5453,2))

Result:

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:

Python program:

#The round() with list of floats



float_lst = [10.56, 11.4, 30.456, 40.8, 50.55]

rounded_list = [round(x) for x in float_lst]



print("List before round: ",float_lst)

print("List after round: ",rounded_list)

Output:

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):

float_lst = [10.5678, 11.456545, 30.4561245, 40.845474, 50.55456474]

rounded_list = [round(x,2) for x in float_lst]



print("List before round: ",float_lst)

print("List after round with precision: ",rounded_list)

Output:

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:

Code:

#The round() for Fahrenheit to Celsius conversion

Fahrenheit_List = [98, 102, 110, 125]

Celsius_List_simple = map(lambda F: ((5.0/9.0)*(F - 32),2), Fahrenheit_List)

Celsius_List_rounded = map(lambda F: round(((5.0/9.0)*(F - 32)),1), Fahrenheit_List)


print("Celsius List default: ", list(Celsius_List_simple))

print("Celsius List with round function: ", list(Celsius_List_rounded))

Output:

round F-to-C

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

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!