[Tutor] print question

Dick Moores rdm at rcblue.com
Tue Oct 9 03:01:14 CEST 2007


def secsToHMS(seconds):
     """
     Convert seconds to hours:minutes:seconds, with seconds rounded 
to nearest hundredths of a second, and print
     """
     hours, minutes = 0, 0
     if seconds >= 60 and seconds < 3600:
         minutes, seconds = divmod(seconds, 60)
     elif seconds >= 3600:
         hours, seconds = divmod(seconds, 3600)
         minutes, seconds = divmod(seconds, 60)
     seconds = str(round(seconds,2))
     print "%d:%d:%s" % (int(hours), int(minutes), seconds)

secsToHMS(18456.876012)

This prints 5:7:36.88

What's the best way to get hours in 2 or more digits, and minutes in 
2 digits, so that the above would be 05:07:36.88? (I'm writing a stopwatch.)

Thanks,

Dick Moores



More information about the Tutor mailing list