Python 2 - How would you round up/down to the nearest 6 minutes? -


there numerous examples of people rounding nearest ten minutes can't figure out logic behind rounding nearest six. thought matter of switching few numbers around can't work.

the code i'm working located @ my github. block i've got isn't close working (won't give output) is:

def companytimer():     if minutes % 6 > .5:         companyminutes = minutes + 1     elif minutes % 6 < 5:         companyminutes = minutes - 1     else:         companyminutes = minutes     print companyminutes 

looking @ now, see logic incorrect - if working, add , subtract 1 minute portion of code doesn't make sense.

anyway, have no idea how remedy - point me in right direction, please?

ps - i'm making personal use @ work.. not asking job me keep track of hours @ work. don't want there issues that.

thanks!

here's general function round nearest x:

def round_to_nearest(num, base):     n = num + (base//2)     return n - (n % base)  [round_to_nearest(i, 6) in range(20)] # [0, 0, 0, 6, 6, 6, 6, 6, 6, 12, 12, 12, 12, 12, 12, 18, 18, 18, 18, 18] 

explanation:

  • n % base remainder left on when dividing n base. known modulo operator.
  • simply subtracting num%6 num give 0 0-5, 6 6-11, , on.
  • since want "round" instead of "floor", can bias result adding half of base (base//2) beforehand.

Popular posts from this blog