Member Avatar for cyon

Given s.ss or seconds accurate to the hundredth place, what is the most effective way to convert it to the string mm:ss.s?

Here is my current attempt:

import time

def convert(t):
	print time.strftime("%M:%S",time.gmtime(round(t,1)))+str((".%01d" % (((round(t,1))-int(round(t,1)))*10)))

t = float(raw_input('Enter time in s.ss: '))
convert(t)

However, the results don't seem quite right:

>>>
Enter time in s.ss: 2.84
00:02.7

It seems like round(t,1) doesn't do its job right:

>>> round(2.84,1)
2.7999999999999998

Any suggestions?

Thank you for your help,
cyon

Other problems:
- strftime doesn't round any of the decimals

Recommended Answers

All 6 Replies

Here is adapted to you and my own use version of timestr function.

Notice that normal use of time for logging etc becomes easier when can put only timestr() for time values and have fuction to make it human understandable. Use example line+='Processing took '+timestr(t2)+'.\nFinnishing time :' +time.asctime()+'.\n'

import time

def timestr(t):
    if t>60: return "%i min %.3f s" % divmod(t,60)
    elif t>1: return "%.3f s" % t
    else: return "%i ms" % int(t*1000)


def convert(t):
    return "%i:%.2f s" % divmod(t,60)

t = float(raw_input('Enter time in s.ss: '))
print convert(t)
Member Avatar for cyon

Here is adapted to you and my own use version of timestr function.

Notice that normal use of time for logging etc becomes easier when can put only timestr() for time values and have fuction to make it human understandable. Use example line+='Processing took '+timestr(t2)+'.\nFinnishing time :' +time.asctime()+'.\n'

import time

def timestr(t):
    if t>60: return "%i min %.3f s" % divmod(t,60)
    elif t>1: return "%.3f s" % t
    else: return "%i ms" % int(t*1000)


def convert(t):
    return "%i:%.2f s" % divmod(t,60)

t = float(raw_input('Enter time in s.ss: '))
print convert(t)

Thank you for your reply and for sharing your code, but it doesn't quite accomplish my original request. I am trying to convert, with appropriate rounding, a float in the format of s.ss to a string that is mm:ss.s, e.g., (using > to denote the conversion)

2.84 > 00:02.8
2.89 > 00:02.9
59.99 > 01:00.0

The need for exactness is due to the fact that the original floats are the racing times of a game.

I am now trying to use divmod and strftime to produce the desired results and will post back if I make any progress...

EDIT:
This seems to work, though lines 5-13 are inelegant:

def convert(time):
	t = round(time,1)
	minutes , seconds = divmod(t , 60)
	
	if minutes < 10:
		minutes = '0%.f' % minutes
	else:
		minutes = '%.f' % minutes
	
	if seconds < 10:
		seconds = '0%.1f' % seconds
	else:
		seconds = '%.1f' % seconds
	
	return "%s:%s" % (minutes , seconds)

time = float(raw_input('Enter time in s.ss: '))
print convert(time)

I do not know Pythons standard library yet well, but old BASIC hacker would do

""" Desired results
Input  Output
2.84 > 00:02.8
2.89 > 00:02.9
59.99 > 01:00.0
"""
def convert1(t):
    ## desisecond rounding
    t= int(t*10+0.5)  ## add half to get rounding to desisecond (funny unit BTW)
    minutes, ds = divmod(t,60*10)
    minutes=minutes % 10
    s,ds= divmod(ds, 10)
    return (minutes,s,ds)

def timestr(minutes,s,ds):
    return str(minutes).zfill(2)+':'+str(s).zfill(2)+'.'+str(ds).zfill(1)

for f in [2.84, 2.89, 59.99]:
    t=convert1(f) ## get minutes and desiseconds
    print f,'->',timestr(*t)    
""" Output:
>>> 
2.84 -> 00:02.8
2.89 -> 00:02.9
59.99 -> 01:00.0
"""

I do not know Pythons standard library yet well, but old BASIC hacker would do

""" Desired results
Input  Output
2.84 > 00:02.8
2.89 > 00:02.9
59.99 > 01:00.0
"""
def convert1(t):
    ## desisecond rounding
    t= int(t*10+0.5)  ## add half to get rounding to desisecond (funny unit BTW)
    minutes, ds = divmod(t,60*10)
    minutes=minutes % 10
    s,ds= divmod(ds, 10)
    return (minutes,s,ds)

def timestr(minutes,s,ds):
    return str(minutes).zfill(2)+':'+str(s).zfill(2)+'.'+str(ds).zfill(1)

for f in [2.84, 2.89, 59.99]:
    t=convert1(f) ## get minutes and desiseconds
    print f,'->',timestr(*t)    
""" Output:
>>> 
2.84 -> 00:02.8
2.89 -> 00:02.9
59.99 -> 01:00.0
"""

I suggest

timestr = "{0:0>2}:{1:0>2}.{2:0>1}".format

for python >= 2.6

Member Avatar for cyon

Thanks for the replies and for introducing me to zfill() and string formatting.

Revision of the code

def convert(time):
	t = round(time,1)
	minutes , seconds = divmod(t , 60)

	minutes = ('%.f' % minutes).zfill(2)
	seconds = ('%.1f' % seconds).zfill(4)
	return "%s:%s" % (minutes,seconds)

for f in [2.84, 2.89, 59.99]:print f,'>',convert(f)
"""
>>> 
2.84 > 00:02.8
2.89 > 00:02.9
59.99 > 01:00.0
>>> """
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.