Python – Round Number to Nearest 10
To round number to nearest 10, use round() function. We can divide the value by 10, round the result to zero precision, and multiply with 10 again. Or you can pass a negative value for precision. The negative denotes that rounding happens to the left of the decimal point.
In this tutorial, we will write Python programs to round a number to its nearest 10 or 100.
For example, if the number is 3652, then its nearest number to 10 is 3650. And the nearest 100 is 3700.
Example 1 – Round Number to Nearest 10 using round()
In this example, we will read a number from user, and round the value to its nearest 10 using round() function.
Python Program
number = int(input('Enter a number :'))
rounded = round(number/10)*10
print('Rounded Number :', rounded)
Output
Enter a number :3652
Rounded Number : 3650
Or you can also provide a negative number as the second argument for round() function, to round to those number of digits before decimal point.
Python Program
number = int(input('Enter a number :'))
rounded = round(number, -1)
print('Rounded Number :', rounded)
Output
Enter a number :3652
Rounded Number : 3650
Example 2 – Round Number to Nearest 100 using round()
In this example, we will round the number to its nearest 100 using round() function.
Python Program
number = int(input('Enter a number :'))
rounded = round(number/100)*100
print('Rounded Number :', rounded)
Output
Enter a number :3652
Rounded Number : 3700
Or you can also provide a negative number as the second argument for round() function, to round to those number of digits before decimal point. To round to nearest 100, we need to provide -2 as second argument to round().
Python Program
number = int(input('Enter a number :'))
rounded = round(number, -2)
print('Rounded Number :', rounded)
Output
Enter a number :3652
Rounded Number : 3700
Conclusion
Concluding this Python Tutorial, we learned how to use round() function to round a number to nearest 10 or 100. You can use the same process to find the nearest number to any digit before the decimal point.