Round a number to the nearest 500 in Python

avatar

Borislav Hadzhiev

Last updated: Jul 11, 2022

banner

Check out my new book

Table of Contents #

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

Round a number to the nearest 500 in Python #

To round a number to the nearest 500:

  1. Call the round() function passing it the number divided by 500.
  2. Multiply the result by 500.
  3. The result of the calculation is the number rounded to the nearest 500.
main.py
import math # ✅ Round number to nearest 500 def round_to_nearest_500(num): return round(num / 500) * 500 print(round_to_nearest_500(777)) # 👉️ 1000 print(round_to_nearest_500(1)) # 👉️ 0 print(round_to_nearest_500(1400)) # 👉️ 1500 # -------------------------------------- # ✅ Round number UP to nearest 500 def round_up_to_nearest_500(num): return math.ceil(num / 500) * 500 print(round_up_to_nearest_500(640)) # 👉️ 1000 print(round_up_to_nearest_500(1)) # 👉️ 500 # -------------------------------------- # ✅ Round number DOWN to nearest 500 def round_down_to_nearest_500(num): return math.floor(num / 500) * 500 print(round_down_to_nearest_500(999)) # 👉️ 500 print(round_down_to_nearest_500(1840)) # 👉️ 1500

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

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

main.py
print(round(13.4)) # 👉️ 13 print(round(13.6)) # 👉️ 14

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

main.py
print(1750 / 500) # 👉️ 3.5 print(1400 / 500) # 👉️ 2.8 print(round(1750 / 500)) # 👉️ 4 print(round(1400 / 500)) # 👉️ 3 print(round(1750 / 500) * 500) # 👉️ 2000 print(round(1400 / 500) * 500) # 👉️ 1500

This is a two step process:

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

Round a number Up to the nearest 500 in Python #

To round a number up to the nearest 500:

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

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

main.py
import math print(math.ceil(456.001)) # 👉️ 457 print(math.ceil(456.999)) # 👉️ 457
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 hundred.

main.py
import math print(1346 / 500) # 👉️ 2.692 print(1600 / 500) # 👉️ 3.2 print(math.ceil(1346 / 500)) # 👉️ 3 print(math.ceil(1600 / 500)) # 👉️ 4 print(math.ceil(1346 / 500) * 500) # 👉️ 1500 print(math.ceil(1600 / 500) * 500) # 👉️ 2000

This is a two step process:

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

Round a Number Down to the nearest 500 in Python #

To round a number down to the nearest 500:

  1. Call the math.floor() method passing it the number divided by 500.
  2. Multiply the result by 500.
  3. The result of the calculation is the number rounded down to the nearest 500.
main.py
import math def round_down_to_nearest_500(num): return math.floor(num / 500) * 500 print(round_down_to_nearest_500(999)) # 👉️ 500 print(round_down_to_nearest_500(1840)) # 👉️ 1500 print(round_down_to_nearest_500(2840)) # 👉️ 2500

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

main.py
import math print(math.floor(25.999)) # 👉️ 25 print(math.floor(25.001)) # 👉️ 25
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 500.

main.py
import math print(4880 / 500) # 👉️ 9.76 print(2510 / 500) # 👉️ 5.02 print(math.floor(4880 / 500)) # 👉️ 9 print(math.floor(2510 / 500)) # 👉️ 5 print(math.floor(4880 / 500) * 500) # 👉️ 4500 print(math.floor(2510 / 500) * 500) # 👉️ 2500

This is a two step process:

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