Syntax of using round function
The round() function in Python takes two parameters:
- The round() function returns the rounded value which is closest to the integer to its given value if ndigits is not provided.
- The ndigits specify 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:
- 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:
The returned result is 25.54 as the next digit is less than 5. What if it was 5 or more, see the next example.
Code:
#The round() function with number and ndigit parameters print("The round value for 25.5453 = ",round(25.5453,2))
Result:
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:
List after round: [11, 11, 30, 41, 51]
Using precision in the round function for list
And of course, you may apply the precision in the 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:
More practical example – using round function for Fahrenheit to Celsius conversion
- As converting Fahrenheit to Celsius in Python programs, it returns big numbers with a decimal point.
- The round function can be useful in that scenario.
- I have shown Fahrenheit to Celsius conversion in other tutorials (map, lambda) and converted numbers like 36.66666666666667, 38.88888888888889.
- In this example, the round function is used in the lambda function to get 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:
List after round with precision: [10.57, 11.46, 30.46, 40.85, 50.55]
You can see, the precision value is given as 1.