Python round

The Python round function rounds the specified expression or number to the nearest integer. The syntax of the round Function in Python Programming is

round(Number, Digits);

If the number argument is a positive or negative number, the Python round function returns the nearest value. And if is not a number, the math round function returns TypeError.

Digits (Optional): If you omit this argument, the round Function round the Number to the nearest integer value, otherwise, Number rounded to the decimal value specified in Digits arguments.

Python round Function Example

The math round function rounds the number to the nearest integer. In this example, we are going to round the values of different data types.

  • In the first two, we used the Python round Function directly on both the Positive decimals and negative decimals.
  • Next two statements, we used the round Function with two arguments, so they round them to the nearest decimal value in the third position.
  • Following four, we used the round Function on Tuple and List items. If you observe the Python screenshot, this Math function is working correctly on them.
  • Then, We tried math round Function directly on multiple values.
  • Last, We tried round Function on the String value, and it returns TypeError.
# Python round Function

Tup = (10.98, 20.26, 30.05, -40.95 , 50.85) # Tuple Declaration
Lis = [-10.98, 32.65, -39.29, -42.15 , -39.97] # List Declaration

print('round() Function on Positive Decimal = %.2f' %round(10.98763))
print('round() Function on Negative Decimal = %.2f' %round(-15.48476))

print('round() Function with Second argument = %.3f' %round(10.98763, 3))
print('round() Function with Second argument = %.3f' %round(-15.48476, 3))

print('round() Function on Tuple Item = %d' %round(Tup[2]))
print('round() Function on Tuple Item = %d' %round(Tup[4]))
print('round() Function on List Item = %d' %round(Lis[2]))
print('round() Function on List Item = %d' %round(Lis[4]))

print('round() Function on Multiple Number = %.2f' %round(10 + 20 - 40.6578, 2))

print('round() Function on String Value = ', round('2.95'))
Python ROUND Function