Register Login

How to Round in Python

Updated Aug 05, 2019

Round() function in Python: Definition

The round() is an in-built function in Python that is used for rounding off a number and returns a floating-point number. If the number of digits up to which it is to be rounded off is given else returns the nearest integer value if no such number is mentioned.

Syntax

The syntax of the round function is as follows:

round(number[digits])

Parameters

The parameters of the round function are:

  • number -Specifies the number that has to be rounded off.
  • digits (optional) -Used to mention the number of digits up to which the number needs to be rounded. If this argument is not provided, the number is rounded off to the nearest integer value.

Here, the number specifies the number that has to be rounded off. The other argument digits are optional, that is used to mention the number of digits up to which the number needs to be rounded. If this argument is not provided, the number is rounded off to the nearest integer value.

Return Value

The round() function returns two types of values:

  • It returns a value of the number that is rounded off to the nearest integer if the digits argument is not provided
  • If the digits argument is provided, the number is rounded off to the specified digits. In that case, it will be rounded off to the multiple that is close to 10 raised to the power minus the digits argument.

Example of Python round() functions

Example 1

Input:

print(round(10.88))

Output:

11

The result will be 11 as no other parameters are provided in the function. So as the number after the decimal point is greater than 5, 10.88 is rounded to the nearest whole number ie. 11. If there was a number less than 5 after the decimal; 10.88 would have been rounded to 10.

Let us consider another Input

print(round(15.5422,2))

Output:

15.54

The result will be 15.54. The number is rounded off to two places after decimal as specified in the syntax. But as the digit after .54 is 2 and is less than 5, the number remains the same.

Example 2: Round off to the nearest Integer

If a number has to be rounded up to an integer, and the digit parameter is not provided, the number will be rounded to the closest integer.

Input

print (round(10.5))

Output:

11

The result will be 11, as no other parameter is provided. The result would have been the same if the digit parameter given is None.

Example 3: Converting fraction to decimal

The round() function can be used for converting fraction to decimal. Let us consider the following example

a=1/3
print(a)
print(round(a , 2))

Output

0.3333333333333333
0.33

The first result will be 0.33333333333. The second result will be 0.33, as the digit parameter is given as 2.

Example 4: Round off a list of floats

The function can be efficiently used to round off a list of floats. 

f_list = [9.3923, 3.6541, 56.768]
final_list = [round(x,2) for x in f_list]

print("List before round: ",f_list)
print("List after round with precision: ",final_list)

Output:

List before round:  [9.3923, 3.6541, 56.768]                                                                                   
List after round with precision:  [9.39, 3.65, 56.77]

Note: Here all the floats are rounded up to two places after the decimal point according to the digits parameter


×