Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

There are numerous examples of people rounding to the nearest ten minutes but I can't figure out the logic behind rounding to the nearest six. I thought it would be a matter of switching a few numbers around but I can't get it to work.

The code I'm working with is located at my Github. The block I've got that isn't even close to working (won't give any output) is:

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

Looking at it now, I see that my logic is incorrect - even if it were working, the add and subtract 1 minute portion of the code doesn't make sense.

Anyway, I have no idea how to remedy this - could someone point me in the right direction, please?

PS - this is something I'm making for personal use at work.. not asking for help with my job but this will help me keep track of my hours at work. Don't want there to be any issues with that.

Thanks!

like image 851
Ross Wardrup Avatar asked Apr 09 '15 18:04

Ross Wardrup


People also ask

How do you round up and down in Python?

The round() function rounds a number to the nearest whole number. The math. ceil() method rounds a number up to the nearest whole number while the math. floor() method rounds a number down to the nearest whole number.

Does Python round 0.5 up or down?

5 is round up for positive values and round down for negative values. For instance, both round(0.5) and round(-0.5) return 0 , while round(1.5) gives 2 and round(-1.5) gives -2 . This Python behaviour is a bit different from how rounding usually goes.

How do you round up in Python?

Python has a built-in round() function that takes two numeric arguments, n and ndigits , and returns the number n rounded to ndigits . The ndigits argument defaults to zero, so leaving it out results in a number rounded to an integer.


2 Answers

Here's a general function to round to nearest x:

def round_to_nearest(num, base):
    n = num + (base//2)
    return n - (n % base)

[round_to_nearest(i, 6) for i 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 is the remainder left over when dividing n by base. Also known as the modulo operator.
  • Simply subtracting num%6 from num would give you 0 for 0-5, 6 for 6-11, and so on.
  • Since we want to "round" instead of "floor", we can bias this result by adding half of the base (base//2) beforehand.
like image 69
tzaman Avatar answered Sep 24 '22 03:09

tzaman


If you want to round to the nearest 7 minutes (e.g. round7(3.6) => 7, round7(17.4) => 14), you can make use of Python's built in round function like so:

def round7(value):
    return round(value / 7.0) * 7.0

>>> round7(17.4999)
14.0
>>> round7(17.5001)
21.0

or, a more general function:

def round_to_nearest(increment, value):
    return round(value / float(increment)) * increment

>>> round_to_nearest(7, 17.4999)
14.0
>>> round_to_nearest(6, 21.0001)
24.0
like image 45
Jake Griffin Avatar answered Sep 25 '22 03:09

Jake Griffin