# How to round numbers in Python

Round numbers in Python – The round( ) function in Python takes an integer and rounds it to a given number of decimal places i-e: round numbers in Python. Round( ) rounds an integer to zero decimal places by default. If you need to work with numbers with a specific level of precision, round( ) comes in useful.

Practical application:

1. When working with money, rounding numbers is one of the most typical applications. This is due to the fact that many individuals prefer not to work with penny fractions or decimal fractions when dealing with money.
2. When working with mathematical calculations that yield unrounded values, rounding numbers is often frequent. For this reason, rounding values is frequently use in data science and machine learning.

## Python round() Method

The round( ) function in Python takes an integer and rounds it to a particular decimal place. This function delivers a floating-point value that has been round according to your preferences. The round() function rounds an integer to zero decimal places by default.

Syntax:

``round(number, number of digits)``

round( ) parameters:

• number – number to be rounded
• number of digits (Optional) – number of digits

## round( ) python Examples:

When the second parameter is present, then it returns:

• When the (ndigit+1)th digit is more than 5, the last decimal digit is raise by one, otherwise it remains the same.

If the second parameter is present, the round() method in Python is implemented as follows.

``print(round(2.555, 2))print(round(2.786, 2))print(round(2.553, 2))``

#### Output:

``2.562.792.55``

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

• If just an integer is provided, such as less then 15, it will be rounded to 15.
• A decimal number is supply, it will round off to the ceiling integer if the decimal value is greater than 5, and to the floor integer if the decimal value is less than 5.

If the second parameter is omitted, the round() method in Python is implemented as follows.

``print(round(15))print(round(51.6))print(round(51.5))print(round(51.4))``

#### Output:

``15525251``

Error and Exceptions:
TypeError : When the arguments contain anything other than numbers, this error is thrown.

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

#### Output:

``Traceback (most recent call last):File "/ranjeet/softhunt.py", line 1, inprint(round("a", 2))TypeError: type str doesn't define round method``

Practical Applications:

Handling the discrepancy between fractions and decimals is one of the most prevalent uses of rounding functions. When converting 1/3 to decimal, one usage of rounding numbers is to abbreviate the threes to the right of the decimal point. And working with 1/3 in decimal, you’ll almost always use the rounded figures 0.33 or 0.333. When there is no precise equivalent to the fraction in decimal, you generally deal with merely two or three digits to the right of the decimal point. In decimal, how would you represent 1/6? Don’t forget to round up!

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

#### Output:

``0.33333333333333330.33``

## Conclusion:

If we round off integers to the floor or ceiling without supplying the second argument in Python, it will yield 15.0 in Python 3 and 15 in Python 3, therefore we can avoid this by using (int) type conversion in Python. It’s also worth noting that the round( ) method behaves strangely when calculating the mean of two integers.