首页 > 解决方案 > 浮点值到时间(HH:MM)格式

问题描述

hr = int(input("Enter period of earth rotation : "))

ln = float(input("Enter value of longitude : "))

calc = (hr/360)*ln

print (calc)

输入:-24,82.50

我希望输出是5:30, 而不是5.5

标签: pythonpython-3.x

解决方案


您已经得到calc,以小时为单位的确切时间,您需要将十进制转换为某个分钟数。我会通过分别存储小时和分钟来做到这一点:

hours = int(calc)
minutes = int((calc - hours) * 60)
print(f"{hours}:{minutes:02}")

推荐阅读