round to nearest multiple of 5 python

Solutions on MaxInterview for round to nearest multiple of 5 python by the best coders in the world

showing results for - "round to nearest multiple of 5 python"
Miguel
21 Nov 2019
1# Basic syntax:
2import math
3math.floor(larger_number / multiple) * multiple
4
5# Example usage:
6# Say you want to get the nearest multiple of 5 less than 29
7import math
8math.floor(29 / 5) * 5
9--> 25
10
11# Note, you can also do this with div if you don't want to import math
1229 // 5 * 5
13
14# Note, to get the nearest multiple of 5 greater than 29 run:
15math.ceil(29 / 5) * 5
16--> 30
Agustín
31 Jun 2020
1def rof(x):   '''round up to multiple of 5'''
2    if x%5==4:
3        x+=1
4    elif x%5==3:
5        x+=2
6    print(x)