Round a number to the nearest 5 in Python

avatar

Borislav Hadzhiev

Last updated: Jul 11, 2022

banner

Photo from Unsplash

Table of Contents #

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

Round a number to the nearest 5 in Python #

To round a number to the nearest 5:

  1. Call the round() function passing it the number divided by 5.
  2. Multiply the result by 5.
  3. The result of the calculation is the number rounded to the nearest 5.
main.py
import math # ✅ Round number to nearest 5 def round_to_nearest_5(num): return round(num / 5) * 5 print(round_to_nearest_5(2)) # 👉️ 0 print(round_to_nearest_5(8)) # 👉️ 10 print(round_to_nearest_5(26)) # 👉️ 25 # -------------------------------------- # ✅ Round number UP to nearest 5 def round_up_to_nearest_5(num): return math.ceil(num / 5) * 5 print(round_up_to_nearest_5(23)) # 👉️ 25 print(round_up_to_nearest_5(57)) # 👉️ 60 # -------------------------------------- # ✅ Round number DOWN to nearest 5 def round_down_to_nearest_5(num): return math.floor(num / 5) * 5 print(round_down_to_nearest_5(121)) # 👉️ 120 print(round_down_to_nearest_5(129)) # 👉️ 125

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

When passed a single argument, the round function rounds to the nearest integer.

main.py
print(round(14.4)) # 👉️ 14 print(round(14.6)) # 👉️ 15

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

main.py
print(24 / 5) # 👉️ 4.8 print(38 / 5) # 👉️ 7.6 print(round(24 / 5)) # 👉️ 5 print(round(38 / 5)) # 👉️ 8 print(round(24 / 5) * 5) # 👉️ 25 print(round(38 / 5) * 5) # 👉️ 40

This is a two step process:

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

Round a number Up to the nearest 5 in Python #

To round a number up to the nearest 5:

  1. Call the math.ceil() method passing it the number divided by 5.
  2. Multiply the result by 5.
  3. The result of the calculation is the number rounded up to the nearest five.
main.py
import math def round_up_to_nearest_5(num): return math.ceil(num / 5) * 5 print(round_up_to_nearest_5(23)) # 👉️ 25 print(round_up_to_nearest_5(57)) # 👉️ 60

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

main.py
import math print(math.ceil(6.01)) # 👉️ 7 print(math.ceil(6.99)) # 👉️ 7
If the passed in number has a fractional part, the math.ceil method rounds the number up.

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

main.py
import math print(142 / 5) # 👉️ 28.4 print(148 / 5) # 👉️ 29.6 print(math.ceil(142 / 5)) # 👉️ 29 print(math.ceil(148 / 5)) # 👉️ 30 print(math.ceil(142 / 5) * 5) # 👉️ 145 print(math.ceil(148 / 5) * 5) # 👉️ 150

This is a two step process:

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

Round a Number Down to the nearest 5 in Python #

To round a number down to the nearest 5:

  1. Call the math.floor() method passing it the number divided by 5.
  2. Multiply the result by 5.
  3. The result of the calculation is the number rounded down to the nearest 5.
main.py
import math def round_down_to_nearest_5(num): return math.floor(num / 5) * 5 print(round_down_to_nearest_5(121)) # 👉️ 120 print(round_down_to_nearest_5(129)) # 👉️ 125

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

main.py
import math print(math.floor(3.99)) # 👉️ 3 print(math.floor(3.01)) # 👉️ 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 5.

main.py
import math print(49 / 5) # 👉️ 9.8 print(56 / 5) # 👉️ 11.2 print(math.floor(49 / 5)) # 👉️ 9 print(math.floor(56 / 5)) # 👉️ 11 print(math.floor(49 / 5) * 5) # 👉️ 45 print(math.floor(56 / 5) * 5) # 👉️ 55

This is a two step process:

  1. Divide the number by 5 and round the result down to the nearest integer.
  2. Multiply the result by 5 to get the number rounded down to the nearest 5.
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.