如何将浮动转换为小时分秒? [英] How to convert float into Hours Minutes Seconds?

查看:39
本文介绍了如何将浮动转换为小时分秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有浮点值,我试图将它们转换为小时:分钟:秒,但我失败了.我关注了以下帖子:

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:

将浮点数转换为 hh:mm 格式

例如我有一个浮点格式的值:

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))

它给了我输出:

00:36 

但实际上它应该像00:00:36".我如何得到这个?

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

推荐答案

Divmod 函数只接受两个参数,因此你得到两个参数中的一个Divmod()

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)

这篇关于如何将浮动转换为小时分秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆