Search code examples
pythontimefloating

How to convert float into Hours Minutes Seconds?


I've values in float and I am trying to convert them into Hours:Min:Seconds but I've failed. I've followed the following post:

Converting a float to hh:mm format

For example I've got a value in float format:

time=0.6 

result = '{0:02.0f}:{1:02.0f}'.format(*divmod(time * 60, 60))

and it gives me the output:

00:36 

But actually it should be like "00:00:36". How do I get this?


Solution

  • Divmod function accepts only two parameter hence you get either of the two Divmod()

    So you can try doing this:

    time = 0.6
    mon, sec = divmod(time, 60)
    hr, mon = divmod(mon, 60)
    print "%d:%02d:%02d" % (hr, mon, sec)