Round a number to the nearest 5, 10, 100, 1000 in Python

avatar
Borislav Hadzhiev

Last updated: Feb 20, 2023
16 min

banner

# Table of Contents

  1. Round a float to the nearest 10th (0.1) in Python
  2. Round a float to the nearest 0.5 in Python
  3. Round a number to the nearest 5 in Python
  4. Round a number to the nearest 10 in Python
  5. Round a number to the nearest 100 in Python
  6. Round a number to the nearest 500 in Python
  7. Round a number to the nearest 1000 in Python
  8. Round a number to the nearest even number in Python
Make sure to click on the correct subheading depending on how you need to round the number.

# Round a float to the nearest 10th (0.1) in Python

Use the round() function to round a float to the nearest 10th (0.1), e.g. result = round(4.5678, 1).

The round() function will return the number rounded to 1-digit precision after the decimal point.

main.py
# โœ… round a float to the nearest 10th (0.1) result_1 = round(4.5678, 1) print(result_1) # ๐Ÿ‘‰๏ธ 4.6 result_2 = round(4.1234, 1) print(result_2) # ๐Ÿ‘‰๏ธ 4.1 # ------------------------------- # โœ… print a float rounded to the nearest 10th (0.1) my_float = 4.5678 my_str_1 = f'{my_float:.1f}' print(my_str_1) # ๐Ÿ‘‰๏ธ '4.6' my_str_2 = f'{my_float:.2f}' print(my_str_2) # ๐Ÿ‘‰๏ธ '4.57'

round float to nearest tenth

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)
The round function returns the number rounded to ndigits precision after the decimal point.

If ndigits is omitted, the function returns the nearest integer.

main.py
my_num = 3.456 result_1 = round(my_num) print(result_1) # ๐Ÿ‘‰๏ธ 3 result_2 = round(my_num, 1) print(result_2) # ๐Ÿ‘‰๏ธ 3.5

If you need to print a floating-point number rounded to the nearest 10th (0.1), use a formatted string literal.

main.py
my_float = 4.5678 my_str_1 = f'{my_float:.1f}' print(my_str_1) # ๐Ÿ‘‰๏ธ '4.6' my_str_2 = f'{my_float:.2f}' print(my_str_2) # ๐Ÿ‘‰๏ธ '4.57'
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # ๐Ÿ‘‰๏ธ is subscribed: True

Make sure to wrap expressions in curly braces - {expression}.

We are also able to use the format specification mini-language in expressions in f-strings.

main.py
my_float = 1.45678 result_1 = f'{my_float:.1f}' print(result_1) # ๐Ÿ‘‰๏ธ '1.5' result_2 = f'{my_float:.2f}' print(result_2) # ๐Ÿ‘‰๏ธ '1.46' result_3 = f'{my_float:.3f}' print(result_3) # ๐Ÿ‘‰๏ธ '1.457'

The f character between the curly braces stands for fixed-point notation.

The character formats the number as a decimal number with the specified number of digits following the decimal point.

# Table of Contents

  1. Round a float to the nearest 0.5 in Python
  2. Round a number to the nearest 5 in Python
  3. Round a number to the nearest 10 in Python
  4. Round a number to the nearest 100 in Python
  5. Round a number to the nearest 500 in Python
  6. Round a number to the nearest 1000 in Python
  7. Round a number to the nearest even number in Python

# Round a float to the nearest 0.5 in Python

To round a float to the nearest 0.5:

  1. Call the round() function passing it the number multiplied by 2.
  2. Divide the result by 2.
  3. The result of the calculation is the number rounded to the nearest 0.5.
main.py
import math # โœ… Round number to nearest 0.5 def round_to_nearest_half_int(num): return round(num * 2) / 2 print(round_to_nearest_half_int(3.1)) # ๐Ÿ‘‰๏ธ 3.0 print(round_to_nearest_half_int(3.7)) # ๐Ÿ‘‰๏ธ 3.5 # -------------------------------------- # โœ… Round number UP to nearest 0.5 def round_up_to_nearest_half_int(num): return math.ceil(num * 2) / 2 print(round_up_to_nearest_half_int(3.1)) # ๐Ÿ‘‰๏ธ 3.5 print(round_up_to_nearest_half_int(3.7)) # ๐Ÿ‘‰๏ธ 4.0 # -------------------------------------- # โœ… Round number DOWN to nearest 0.5 def round_down_to_nearest_half_int(num): return math.floor(num * 2) / 2 print(round_down_to_nearest_half_int(3.9)) # ๐Ÿ‘‰๏ธ 3.5 print(round_down_to_nearest_half_int(3.4)) # ๐Ÿ‘‰๏ธ 3.0

round number to nearest 0 5

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

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

main.py
print(round(7.4)) # ๐Ÿ‘‰๏ธ 7 print(round(7.6)) # ๐Ÿ‘‰๏ธ 8

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

main.py
print(3.1 * 2) # ๐Ÿ‘‰๏ธ 6.2 print(3.7 * 2) # ๐Ÿ‘‰๏ธ 7.4 print(round(3.1 * 2)) # ๐Ÿ‘‰๏ธ 6 print(round(3.7 * 2)) # ๐Ÿ‘‰๏ธ 7 print(round(3.1 * 2) / 2) # ๐Ÿ‘‰๏ธ 3.0 print(round(3.7 * 2) / 2) # ๐Ÿ‘‰๏ธ 3.5

This is a two-step process:

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

# Round a float Up to the nearest 0.5 in Python

Use the math.ceil() method if you need to round a float up to the nearest 0.5.

main.py
import math def round_up_to_nearest_half_int(num): return math.ceil(num * 2) / 2 print(round_up_to_nearest_half_int(3.1)) # ๐Ÿ‘‰๏ธ 3.5 print(round_up_to_nearest_half_int(3.7)) # ๐Ÿ‘‰๏ธ 4.0 print(round_up_to_nearest_half_int(16.2)) # ๐Ÿ‘‰๏ธ 16.5

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

main.py
import math print(math.ceil(3.1)) # ๐Ÿ‘‰๏ธ 4 print(math.ceil(3.9)) # ๐Ÿ‘‰๏ธ 4
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 0.5.

main.py
import math print(6.1 * 2) # ๐Ÿ‘‰๏ธ 12.2 print(6.6 * 2) # ๐Ÿ‘‰๏ธ 13.2 print(math.ceil(6.1 * 2)) # ๐Ÿ‘‰๏ธ 13 print(math.ceil(6.6 * 2)) # ๐Ÿ‘‰๏ธ 14 print(math.ceil(6.1 * 2) / 2) # ๐Ÿ‘‰๏ธ 6.5 print(math.ceil(6.6 * 2) / 2) # ๐Ÿ‘‰๏ธ 7.0

This is a two-step process:

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

# Round a float Down to the nearest 0.5 in Python

Use the math.floor() method to round a number down to the nearest 0.5.

main.py
import math def round_down_to_nearest_half_int(num): return math.floor(num * 2) / 2 print(round_down_to_nearest_half_int(3.9)) # ๐Ÿ‘‰๏ธ 3.5 print(round_down_to_nearest_half_int(3.4)) # ๐Ÿ‘‰๏ธ 3.0

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

main.py
import math print(math.floor(3.9)) # ๐Ÿ‘‰๏ธ 3 print(math.floor(3.1)) # ๐Ÿ‘‰๏ธ 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 0.5.

main.py
import math print(5.9 * 2) # ๐Ÿ‘‰๏ธ 11.8 print(5.1 * 2) # ๐Ÿ‘‰๏ธ 10.2 print(math.floor(5.9 * 2)) # ๐Ÿ‘‰๏ธ 11 print(math.floor(5.1 * 2)) # ๐Ÿ‘‰๏ธ 10 print(math.floor(5.9 * 2) / 2) # ๐Ÿ‘‰๏ธ 5.5 print(math.floor(5.1 * 2) / 2) # ๐Ÿ‘‰๏ธ 5.0

This is a two-step process:

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

# Table of Contents

  1. Round a number to the nearest 5 in Python
  2. Round a number to the nearest 10 in Python
  3. Round a number to the nearest 100 in Python
  4. Round a number to the nearest 500 in Python
  5. Round a number to the nearest 1000 in Python
  6. Round a number to the nearest even number 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

round number to nearest 5

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

Use the math.ceil() method to round a number up to the nearest 5.

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

Use math.floor() if you need to round 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.

# Table of Contents

  1. Round a number to the nearest 10 in Python
  2. Round a number to the nearest 100 in Python
  3. Round a number to the nearest 500 in Python
  4. Round a number to the nearest 1000 in Python
  5. Round a number to the nearest even number in Python

# Round a Number to the nearest 10 in Python

Use the round() function to round a number to the nearest 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

round number to nearest 10

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

Use the math.ceil() method to round 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

Use the math.floor() method to round 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.

# Table of Contents

  1. Round a number to the nearest 100 in Python
  2. Round a number to the nearest 500 in Python
  3. Round a number to the nearest 1000 in Python
  4. Round a number to the nearest even number in Python

# Round a number to the nearest 100 in Python

Use the round() function if you need to round a number to the nearest 100.

main.py
import math # โœ… Round number to nearest 100 num_1 = 237 result_1 = round(num_1, -2) print(result_1) # ๐Ÿ‘‰๏ธ 200 num_2 = 278 result_2 = round(num_2, -2) print(result_2) # ๐Ÿ‘‰๏ธ 300 # -------------------------------------- # โœ… Round number UP to nearest 100 def round_up_to_nearest_100(num): return math.ceil(num / 100) * 100 print(round_up_to_nearest_100(311)) # ๐Ÿ‘‰๏ธ 400 print(round_up_to_nearest_100(1)) # ๐Ÿ‘‰๏ธ 100 # -------------------------------------- # โœ… Round number DOWN to nearest 100 def round_down_to_nearest_100(num): return math.floor(num / 100) * 100 print(round_down_to_nearest_100(546)) # ๐Ÿ‘‰๏ธ 500 print(round_down_to_nearest_100(599)) # ๐Ÿ‘‰๏ธ 500

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

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(346, -1)) # ๐Ÿ‘‰๏ธ 350 print(round(346, -2)) # ๐Ÿ‘‰๏ธ 300

# Round a number Up to the nearest 100 in Python

Use the math.ceil() method if you need to round a number up to the nearest 100.

main.py
import math def round_up_to_nearest_100(num): return math.ceil(num / 100) * 100 print(round_up_to_nearest_100(311)) # ๐Ÿ‘‰๏ธ 400 print(round_up_to_nearest_100(1)) # ๐Ÿ‘‰๏ธ 100

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

main.py
import math print(math.ceil(123.001)) # ๐Ÿ‘‰๏ธ 124 print(math.ceil(123.999)) # ๐Ÿ‘‰๏ธ 124

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 hundred.

main.py
import math print(346 / 100) # ๐Ÿ‘‰๏ธ 3.46 print(600 / 100) # ๐Ÿ‘‰๏ธ 6.0 print(math.ceil(346 / 100)) # ๐Ÿ‘‰๏ธ 4 print(math.ceil(600 / 100)) # ๐Ÿ‘‰๏ธ 6 print(math.ceil(346 / 100) * 100) # ๐Ÿ‘‰๏ธ 400 print(math.ceil(600 / 100) * 100) # ๐Ÿ‘‰๏ธ 600
We first divide the number by 100 and then multiply with 100 to shift 2 decimal places to the right and left, so that math.ceil() works on the hundreds.

This is a two-step process:

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

# Round a Number Down to the nearest 100 in Python

Use the math.floor() method to round a number down to the nearest 100.

main.py
import math def round_down_to_nearest_100(num): return math.floor(num / 100) * 100 print(round_down_to_nearest_100(546)) # ๐Ÿ‘‰๏ธ 500 print(round_down_to_nearest_100(599)) # ๐Ÿ‘‰๏ธ 500 print(round_down_to_nearest_100(775)) # ๐Ÿ‘‰๏ธ 700

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

main.py
import math print(math.floor(15.001)) # ๐Ÿ‘‰๏ธ 15 print(math.floor(15.999)) # ๐Ÿ‘‰๏ธ 15
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 100.

main.py
print(488 / 100) # ๐Ÿ‘‰๏ธ 4.88 print(251 / 100) # ๐Ÿ‘‰๏ธ 2.51 print(math.floor(488 / 100)) # ๐Ÿ‘‰๏ธ 4 print(math.floor(251 / 100)) # ๐Ÿ‘‰๏ธ 2 print(math.floor(488 / 100) * 100) # ๐Ÿ‘‰๏ธ 400 print(math.floor(251 / 100) * 100) # ๐Ÿ‘‰๏ธ 200
We first divide the number by 100 and then multiply with 100 to shift 2 decimal places to the right and left, so that math.floor() works on the hundreds.

This is a two-step process:

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

# Table of Contents

  1. Round a number to the nearest 500 in Python
  2. Round a number to the nearest 1000 in Python
  3. Round a number to the nearest even number in Python

# Round a number to the nearest 500 in Python

Use the round() function to round a number 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

Use the math.ceil() method to round a number 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

Use the math.floor() method to round a number 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.

# Table of Contents

  1. Round a number to the nearest 1000 in Python
  2. Round a number to the nearest even number in Python

# Round a number to the nearest 1000 in Python

Use the round() function to round a number to the nearest 1000.

main.py
import math # โœ… Round number to nearest 1000 num_1 = 4678 result_1 = round(num_1, -3) print(result_1) # ๐Ÿ‘‰๏ธ 5000 num_2 = 4432 result_2 = round(num_2, -3) print(result_2) # ๐Ÿ‘‰๏ธ 4000 # -------------------------------------- # โœ… Round number UP to nearest 1000 def round_up_to_nearest_1000(num): return math.ceil(num / 1000) * 1000 print(round_up_to_nearest_1000(3100)) # ๐Ÿ‘‰๏ธ 4000 print(round_up_to_nearest_1000(1)) # ๐Ÿ‘‰๏ธ 1000 # -------------------------------------- # โœ… Round number DOWN to nearest 1000 def round_down_to_nearest_1000(num): return math.floor(num / 1000) * 1000 print(round_down_to_nearest_1000(5999)) # ๐Ÿ‘‰๏ธ 5000 print(round_down_to_nearest_1000(5004)) # ๐Ÿ‘‰๏ธ 5000
If you need to round a number to the nearest 500, scroll down to the relevant subheading.

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

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, the function rounds to the closest multiple of 10.
  • If ndigits is -2, it rounds to the nearest 100.
  • If ndigits is -3, it rounds to the nearest 1000, etc.
main.py
print(round(3456, -1)) # ๐Ÿ‘‰๏ธ 3460 print(round(3456, -2)) # ๐Ÿ‘‰๏ธ 3500 print(round(3456, -3)) # ๐Ÿ‘‰๏ธ 3000

# Round a number Up to the nearest 1000 in Python

Use the math.ceil() method to round a number up to the nearest 1000.

main.py
import math def round_up_to_nearest_1000(num): return math.ceil(num / 1000) * 1000 print(round_up_to_nearest_1000(3100)) # ๐Ÿ‘‰๏ธ 4000 print(round_up_to_nearest_1000(1)) # ๐Ÿ‘‰๏ธ 1000 print(round_up_to_nearest_1000(2350)) # ๐Ÿ‘‰๏ธ 3000

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

main.py
import math print(math.ceil(1234.001)) # ๐Ÿ‘‰๏ธ 1235 print(math.ceil(1234.999)) # ๐Ÿ‘‰๏ธ 1235
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 thousand.

main.py
import math print(4258 / 1000) # ๐Ÿ‘‰๏ธ 4.258 print(5600 / 1000) # ๐Ÿ‘‰๏ธ 5.6 print(math.ceil(4258 / 1000)) # ๐Ÿ‘‰๏ธ 5 print(math.ceil(5600 / 1000)) # ๐Ÿ‘‰๏ธ 6 print(math.ceil(4258 / 1000) * 1000) # ๐Ÿ‘‰๏ธ 5000 print(math.ceil(5600 / 1000) * 1000) # ๐Ÿ‘‰๏ธ 6000
We first divide the number by 1000 and then multiply with 1000 to shift 3 decimal places to the right and left, so that math.ceil() works on the thousands.

This is a two-step process:

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

# Round a Number Down to the nearest 1000 in Python

Use the math.floor() method to round a number down to the nearest 1000.

main.py
import math def round_down_to_nearest_1000(num): return math.floor(num / 1000) * 1000 print(round_down_to_nearest_1000(5999)) # ๐Ÿ‘‰๏ธ 5000 print(round_down_to_nearest_1000(5004)) # ๐Ÿ‘‰๏ธ 5000 print(round_down_to_nearest_1000(7900)) # ๐Ÿ‘‰๏ธ 7000

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

main.py
import math print(math.floor(13.999)) # ๐Ÿ‘‰๏ธ 13 print(math.floor(13.001)) # ๐Ÿ‘‰๏ธ 13
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 1000.

main.py
import math print(5900 / 1000) # ๐Ÿ‘‰๏ธ 5.9 print(4300 / 1000) # ๐Ÿ‘‰๏ธ 4.3 print(math.floor(5900 / 1000)) # ๐Ÿ‘‰๏ธ 5 print(math.floor(4300 / 1000)) # ๐Ÿ‘‰๏ธ 4 print(math.floor(5900 / 1000) * 1000) # ๐Ÿ‘‰๏ธ 5000 print(math.floor(4300 / 1000) * 1000) # ๐Ÿ‘‰๏ธ 4000
We first divide the number by 1000 and then multiply with 1000 to shift 3 decimal places to the right and left, so that math.floor() works on the thousands.

This is a two-step process:

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

# Round a number to the nearest even number in Python

Use the round() function to round a number to the nearest even number.

main.py
import math # โœ… round number to nearest even number def round_to_nearest_even_number(num): return round(num / 2) * 2 print(round_to_nearest_even_number(3.1)) # ๐Ÿ‘‰๏ธ 4 print(round_to_nearest_even_number(8.6)) # ๐Ÿ‘‰๏ธ 8 # ----------------------------------------------- # โœ… round a number UP to the nearest even number def round_up_to_nearest_even_number(num): return math.ceil(num / 2) * 2 print(round_up_to_nearest_even_number(3.1)) # ๐Ÿ‘‰๏ธ 4 print(round_up_to_nearest_even_number(8.6)) # ๐Ÿ‘‰๏ธ 10 # ----------------------------------------------- # โœ… round a number DOWN to the nearest even number def round_down_to_nearest_even_number(num): return math.floor(num / 2) * 2 print(round_down_to_nearest_even_number(3.1)) # ๐Ÿ‘‰๏ธ 2 print(round_down_to_nearest_even_number(8.6)) # ๐Ÿ‘‰๏ธ 8

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

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

main.py
print(round(22.4)) # ๐Ÿ‘‰๏ธ 22 print(round(22.6)) # ๐Ÿ‘‰๏ธ 23

This is a two-step process:

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

# Round a number Up to the nearest even number in Python

Use the math.ceil() method to round a number up to the nearest even number.

main.py
import math def round_up_to_nearest_even_number(num): return math.ceil(num / 2) * 2 print(round_up_to_nearest_even_number(3.1)) # ๐Ÿ‘‰๏ธ 4 print(round_up_to_nearest_even_number(8.6)) # ๐Ÿ‘‰๏ธ 10

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

main.py
import math print(math.ceil(14.01)) # ๐Ÿ‘‰๏ธ 15 print(math.ceil(14.99)) # ๐Ÿ‘‰๏ธ 15
If the passed-in number has a fractional part, the math.ceil method rounds the number up.

This is a two-step process:

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

# Round a number Down to the nearest even number in Python

Use the math.floor() method to round a number down to the nearest even number.

main.py
import math def round_down_to_nearest_even_number(num): return math.floor(num / 2) * 2 print(round_down_to_nearest_even_number(3.1)) # ๐Ÿ‘‰๏ธ 2 print(round_down_to_nearest_even_number(8.6)) # ๐Ÿ‘‰๏ธ 8

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

main.py
import math print(math.floor(9.99)) # ๐Ÿ‘‰๏ธ 9 print(math.floor(9.01)) # ๐Ÿ‘‰๏ธ 9
If the passed-in number has a fractional part, the math.floor method rounds the number down.

This is a two-step process:

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

I've also written an article on how to round a float to N decimal places.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev