How to round a number to the nearest 5 (closest to multiples of 5) in Python

1 Answer

0 votes
import math 
 
def roundToNearest5(num):
    return math.floor(num / 5) * 5
     
print(roundToNearest5(13))
print(roundToNearest5(14))
print(roundToNearest5(16))

print(roundToNearest5(7))
print(roundToNearest5(3))
print(roundToNearest5(4))
print(roundToNearest5(5))
 
print(roundToNearest5(-1))
print(roundToNearest5(-9))
 
 
 
 
 
'''
run:
 
10
10
15
5
0
0
5
-5
-10

'''

 



answered Jul 17, 2022 by avibootz

Related questions

1 answer 108 views
1 answer 104 views
1 answer 118 views
1 answer 126 views
...