Round a Number to the nearest 10 in Python

avatar

Borislav Hadzhiev

Last updated: Jul 11, 2022

banner

Photo from Unsplash

Table of Contents #

  1. Round a Number to the nearest 10 in Python
  2. Round a Number Up to the nearest 10 in Python
  3. Round a Number Down to the nearest 10 in Python

Round a Number to the nearest 10 in Python #

Use the round() function to round a number to the nearest 10, e.g. result = round(num, -1). When the round() function is called with a second argument of -1, it rounds to the closest multiple of 10.

main.py
import math # ✅ Round number to nearest 10 num_1 = 6 result_1 = round(num_1, -1) print(result_1) # 👉️ 10 num_2 = 4 result_2 = round(num_2, -1) print(result_2) # 👉️ 0 # -------------------------------------- # ✅ Round number UP to nearest 10 def round_up_to_nearest_10(num): return math.ceil(num / 10) * 10 print(round_up_to_nearest_10(3)) # 👉️ 10 print(round_up_to_nearest_10(1)) # 👉️ 10 # -------------------------------------- # ✅ Round number DOWN to nearest 10 def round_down_to_nearest_10(num): return math.floor(num / 10) * 10 print(round_down_to_nearest_10(19)) # 👉️ 10 print(round_down_to_nearest_10(27)) # 👉️ 20

We used the round() function to round a number to the nearest 10.

The round function takes the following 2 parameters:

NameDescription
numberthe number to round to ndigits precision after the decimal
ndigitsthe number of digits after the decimal, the number should have after the operation (optional)
When ndigits is a negative number, the round() function rounds to the left of the decimal.

If ndigits is -1, it rounds to the closest multiple of 10. When ndigits is -2, the function rounds to the nearest 100, etc.

main.py
print(round(157, -1)) # 👉️ 160 print(round(157, -2)) # 👉️ 200

Round a Number Up to the nearest 10 in Python #

To round a number up to the nearest 10:

  1. Call the math.ceil() method passing it the number divided by 10.
  2. Multiply the result by 10.
  3. The result of the calculation is the number rounded up to the nearest 10.
main.py
import math def round_up_to_nearest_10(num): return math.ceil(num / 10) * 10 print(round_up_to_nearest_10(3)) # 👉️ 10 print(round_up_to_nearest_10(1)) # 👉️ 10 print(round_up_to_nearest_10(21)) # 👉️ 30

The math.ceil method returns the smallest integer greater than or equal to the provided number.

main.py
import math print(math.ceil(5.001)) # 👉️ 6 print(math.ceil(5.99)) # 👉️ 6

If the passed in number has a fractional part, the math.ceil method rounds the number up.

main.py
import math my_num = math.ceil(3 / 10) # 👉️ 1 print(my_num) # 👉️ 1 result = my_num * 10 print(result) # 👉️ 10

Here is a step-by-step example of rounding a number up to the nearest 10.

main.py
import math print(21 / 10) # 👉️ 2.1 print(40 / 10) # 👉️ 4.0 print(math.ceil(21 / 10)) # 👉️ 3 print(math.ceil(40 / 10)) # 👉️ 4 print(math.ceil(21 / 10) * 10) # 👉️ 30 print(math.ceil(40 / 10) * 10) # 👉️ 40
We first divide the number by 10 and then multiply with 10 to shift 1 decimal place to the right and left, so that math.ceil() works on the tens.

This is a two step process:

  1. Divide the number by 10 and round the result up to the nearest integer.
  2. Multiply the result by 10 to get the number rounded up to the nearest 10.

Round a Number Down to the nearest 10 in Python #

To round a number down to the nearest 10:

  1. Call the math.floor() method passing it the number divided by 10.
  2. Multiply the result by 10.
  3. The result of the calculation is the number rounded down to the nearest 10.
main.py
import math def round_down_to_nearest_10(num): return math.floor(num / 10) * 10 print(round_down_to_nearest_10(19)) # 👉️ 10 print(round_down_to_nearest_10(27)) # 👉️ 20 print(round_down_to_nearest_10(42)) # 👉️ 40

The math.floor method returns the largest integer less than or equal to the provided number.

main.py
import math print(math.floor(3.999)) # 👉️ 3 print(math.floor(3.001)) # 👉️ 3
If the passed in number has a fractional part, the math.floor method rounds the number down.

Here is a step-by-step example of rounding a number down to the nearest 10.

main.py
import math print(34 / 10) # 👉️ 3.4 print(50 / 10) # 👉️ 5.0 print(math.floor(34 / 10)) # 👉️ 3 print(math.floor(50 / 10)) # 👉️ 5 print(math.floor(34 / 10) * 10) # 👉️ 30 print(math.floor(50 / 10) * 10) # 👉️ 50
We first divide the number by 10 and then multiply with 10 to shift 1 decimal place to the right and left, so that math.floor() works on the tens.

This is a two step process:

  1. Divide the number by 10 and round the result down to the nearest integer.
  2. Multiply the result by 10 to get the number rounded down to the nearest 10.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.