# Round a float to the nearest 0.5 in Python Last updated: Jul 12, 2022 Photo from Unsplash

## 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
```Copied!```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
``````

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
```Copied!```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
```Copied!```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#

To round a float up to the nearest 0.5:

1. Call the `math.ceil()` method passing it the number multiplied by `2`.
2. Divide the result by `2`.
3. The result of the calculation is the number rounded up to the nearest `0.5`.
main.py
```Copied!```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
```Copied!```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
```Copied!```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#

To round a number down to the nearest 0.5:

1. Call the `math.floor()` method passing it the number multiplied by `2`.
2. Divide the result by `2`.
3. The result of the calculation is the number rounded down to the nearest `0.5`.
main.py
```Copied!```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
```Copied!```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
```Copied!```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`.
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.