# Round a number to the nearest even number in Python

Borislav Hadzhiev

Last updated: Jul 14, 2022

Photo from Unsplash

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

To round a number to the nearest even number:

1. Call the round() function, passing it the number divided by 2.
2. Multiply the result by 2.
3. The result of the calculation is the number rounded to the nearest even integer.
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 number UP to 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 number DOWN to 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#

To round a number up to the nearest even number:

1. Call the math.ceil() method passing it the number divided by 2.
2. Multiply the result by 2.
3. The result of the calculation is the number rounded up to the next even integer.
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#

To round a number down to the nearest even number:

1. Call the math.floor() method passing it the number divided by 2.
2. Multiply the result by 2.
3. The result of the calculation is the number rounded down to the nearest even integer.
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 wrote a book in which I share everything I know about how to become a better, more efficient programmer.
You can use the search field on my Home Page to filter through all of my articles.

Copyright © 2022 Borislav Hadzhiev