# round() function in Python

## round() function in Python

Python round() function float point number from the decimal value to the closest multiple of 10.

The int value to the closest multiple of 10 to the power minus ndigits, where ndigits is the precision after the decimal point. If two multiples are equally close, rounding is done toward the even choice.

### Python round() Syntax:

round(number, number of digits)

### Python round() parameters:

• number : number to be rounded
• number of digits (Optional) : number of digits up to which the given number is to be rounded.

If the second parameter is missing, then the round() function returns:

1. if only an integer is given, for instance 15, then it will round off to 15 itself.
2. if a decimal number is given, then it will round off to the closest multiple of 10 to the power minus ndigits

## Python round() example:

### Example 1: Python round() function if the second parameter is missing

 `# for integers` `print``(``round``(``15``))` `# for floating point` `print``(``round``(``51.6``))` `print``(``round``(``51.5``))` `print``(``round``(``51.4``))`

Output:

```15
52
52
51```

When the second parameter is present, then it returns:

The last decimal digit till which it is rounded is increased by 1 when (ndigit+1)th digit is >=5, else it stays the same.

### Example 2: Python round() function if the second parameter is present

 `# when the (ndigit+1)th digit is =5` `print``(``round``(``2.665``, ``2``))` `# when the (ndigit+1)th digit is >=5` `print``(``round``(``2.676``, ``2``))` `# when the (ndigit+1)th digit is <5` `print``(``round``(``2.673``, ``2``))`

Output:

```2.67
2.68
2.67```

### Example 3: Python round() up

 `print``(``round``(``12``))` `print``(``round``(``12.7``))`

Output:

```12
13```

### Example 4: Python round() down

 `print``(``round``(``12``))` `print``(``round``(``12.1``))` `print``(``round``(``12.4``))` `print``(``round``(``12.5``))`

Output:

```12
12
12
12```

## Error and Exceptions

TypeError: This error is raised in the case when there is anything other than numbers in the parameters.

 `print``(``round``(``"a"``, ``2``))`

Output:

```Runtime Errors:
Traceback (most recent call last):
File "/home/ccdcfc451ab046030492e0e758d42461.py", line 1, in
print(round("a", 2))
TypeError: type str doesn't define __round__ method```

## Practical Applications:

One of the common uses of rounding of functions is Handling the mismatch between fractions and decimal.

One use of rounding numbers is to shorten all the three’s to the right of the decimal point in converting 1/3 to decimal. Most of the time, you will use the rounded numbers 0.33 or 0.333 when you need to work with 1/3 in decimal. In fact, you usually work with just two or three digits to the right of the decimal point when there is no exact equivalent to the fraction in decimal. How would you show 1/6 in decimal? Remember to round up!

 `# practical application` `b ``=` `1``/``3` `print``(b)` `print``(``round``(b, ``2``))`

Output:

```0.3333333333333333
0.33```

Note: In python, if we round off numbers to floor or ceil without giving the second parameter, it will return 15.0 for example and in Python 3 it returns 15, so to avoid this we can use (int) type conversion in python. It is also important to note that the round ()function shows unusual behavior when it comes to finding the mean of two numbers.

Last Updated on October 24, 2021 by admin

## Change plot size in Matplotlib – PythonChange plot size in Matplotlib – Python

Change plot size in Matplotlib – Python Plots are an effective way of visually representing data and summarizing it in a beautiful manner. However, if not plotted efficiently it seems appears complicated. In python’s matplotlib provides several libraries for the

## Unpacking a Tuple in PythonUnpacking a Tuple in Python

Unpacking a Tuple in Python Python Tuples In python tuples are used to store immutable objects. Python Tuples are very similar to lists except to some situations. Python tuples are immutable means that they can not be modified in whole program. Packing and

## Python program to print all Prime numbers in an IntervalPython program to print all Prime numbers in an Interval

Given two positive integers start and end. The task is to write a Python program to print all Prime numbers in an Interval. Definition: A prime number is a natural number greater than 1 that has no positive divisors other

## Get a list as input from user – PythonGet a list as input from user – Python

Python – Get a list as input from user We often encounter a situation when we need to take number/string as input from the user. In this article, we will see how to get as input a list from the

## What is the maximum possible value of an integer in Python ?What is the maximum possible value of an integer in Python ?

What is the maximum possible value of an integer in Python ? Consider below Python program. # A Python program to demonstrate that we can store # large numbers in Python x = 10000000000000000000000000000000000000000000 x = x + 1 print

## Python EncapsulationPython Encapsulation

Encapsulation in Python Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly

## Python program to find largest number in a listPython program to find largest number in a list

Given a list of numbers, the task is to write a Python program to find the largest number in given list. Examples: Input : list1 = [10, 20, 4] Output : 20 Input : list2 = [20, 10, 20, 4,

## Python – Using variable outside and inside the class and methodPython – Using variable outside and inside the class and method

Python | Using variable outside and inside the class and method In Python, we can define the variable outside the class, inside the class, and even inside the methods. Let’s see, how to use and access these variables throughout the