decimal time python code example

Example 1: change a decimal to time in datetime python

time = 72.345

hours = int(time)
minutes = (time*60) % 60
seconds = (time*3600) % 60

print("%d:%02d.%02d" % (hours, minutes, seconds))
>> 72:20:42

Example 2: decimal hour to hour minute python

>>> def frac(n):
...     i = int(n)
...     f = round((n - int(n)), 4)
...     return (i, f)
...
>>> frac(53.45)
(53, 0.45) # A tuple 

>>> def frmt(hour): # You can rewrite it with 'while' if you wish
...     hours, _min = frac(hour)
...     minutes, _sec = frac(_min*60)
...     seconds, _msec = frac(_sec*60)
...     return "%s:%s:%s"%(hours, minutes, seconds)
...
>>> frmt(72.345)
'72:20:42'
>>> l = [72.345, 72.629, 71.327]
>>> map(frmt, l)
['72:20:42', '72:37:44', '71:19:37']