def round_up_to_base[x, base=10]:
return x + [base - x] % base
def round_down_to_base[x, base=10]:
return x - [x % base]
which gives
for base=5
:
>>> [i for i in range[20]]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> [round_down_to_base[x=i, base=5] for i in range[20]]
[0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 15, 15, 15, 15, 15]
>>> [round_up_to_base[x=i, base=5] for i in range[20]]
[0, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 15, 15, 15, 15, 15, 20, 20, 20, 20]
for base=10
:
>>> [i for i in range[20]]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> [round_down_to_base[x=i, base=10] for i in range[20]]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
>>> [round_up_to_base[x=i, base=10] for i in range[20]]
[0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20]
tested in Python 3.7.9
Table of Contents #
- Round a number to the nearest 5 in Python
- Round a number Up to the nearest 5 in Python
- 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:
- Call the
round[]
function passing it the number divided by5
. - Multiply the result by
5
. - The result of the calculation is the number rounded to the nearest
5
.
Copied!
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.
Copied!
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.
Copied!
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:
- Divide the number by
5
and round the result to the nearest integer. - Multiply the result by
5
to get the number rounded to the nearest5
.
Round a number Up to the nearest 5 in Python #
To round a number up to the nearest 5:
- Call the
math.ceil[]
method passing it the number divided by5
. - Multiply the result by
5
. - The result of the calculation is the number rounded up to the nearest five.
Copied!
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.
Copied!
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.
Copied!
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:
- Divide the
number by
5
and round the result up to the nearest integer. - Multiply the result by
5
to get the number rounded up to the nearest5
.
Round a Number Down to the nearest 5 in Python #
To round a number down to the nearest 5:
- Call the
math.floor[]
method passing it the number divided by5
. - Multiply the result by
5
. - The result of the calculation is the number rounded down to the nearest
5
.
Copied!
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.
Copied!
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.
Copied!
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:
- Divide the number by
5
and round the result down to the nearest integer. - Multiply the result by
5
to get the number rounded down to the nearest5
.