Python Round Up to the Nearest Ten

Python Round Up to the Nearest Ten

This tutorial will discuss rounding a number to the nearest ten using Python’s ceil() function.

Python Round Number to the Nearest Ten

Python has three built-in functions round(), floor(), and ceil() which can be used to round a number. The ceil() function belongs to the math module, and it is used to round a floating point number to the nearest integer greater than or equal to the given number.

If the significant digit of a floating point number is greater than zero, the number will be increased by one, and if the significant digit is equal to zero, the number will remain the same. For example, let’s use the ceil() function to round a floating point number.

See the code and output below.

from math import ceil

n = 2.1
print(ceil(n))

Output:

3

From the output, the number 2.1 is rounded to 3. The ceil() function can only round a floating point number.

If we want to round an integer to the nearest ten, like 31 to 40, we have to convert the number to a floating point first by dividing it by 10, and then we can pass it to the ceil() function and we can multiply the result with 10 to get the required number. For example, let’s convert a number to the nearest ten.

See the code and output below.

from math import ceil

x = 21
y = ceil(x/10)*10
print(y)

Output:

30

In the above code, the number 21 is rounded to 30. The floor() function of the math module is used to round the given floating point number to the nearest integer which is less than or equal to the given number.

For example, if we use the floor() function in the above code instead of the ceil() function, the result will be 20. The round() function is the mixture of the floor() and ceil() functions, and it rounds a floating point number to the nearest integer, which can be less than, greater than, or equal to the input number.

The function will start from the last significant digit of a floating point number; if it is greater than 5, the current significant digit will be dropped, and the digit before it will be increased by one number. And if the significant digit is less than or equal to 5, the significant digit before it will remain the same.

In other words, the round() function will act as the ceil() function. If the significant digit is greater than 5 and the digit is less than or equal to 5, it will act as the floor() function.

For example, let’s compare the result of all these functions using a single floating point number.

See the code and output below.

from math import ceil, floor

x = 21
y = ceil(x/10)*10
print(y)

y2 = floor(x/10)*10
print(y2)

y3 = round(x/10)*10
print(y3)

Output:

30
20
20

In the above code, we converted the given number to a floating point first and then passed it inside the functions, and then we multiplied the result by 10 to get the final result. Check this link for more details about the ceil() and floor() functions and the math module of Python.

Check this link for more details about the round() function.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook