我将如何将3.65之类的浮点数转换为4分5秒。
我试过使用:
print(datetime.datetime.strptime('3.35','%M%-S'))
但是,我得到了:
ValueError: '-' is a bad directive in format '%-M:%-S'
看一下下面的脚本,您可以弄清楚如何使其工作几天,等等,仅当我们假定格式为“ hours.minutes”时,此方法才有效
import datetime
# Assuming the 3 represents the hours and the 0.65 the minutes
number = 3.65
# First, we need to split the numbero into its whole decimal part
# and its decimal part
whole_decimal_part = hours = int(number) # 3
decimal_part = number % whole_decimal_part # 0.6499999
# Now, we need to know how many extra hours are in the decimal part
extra_hours = round((decimal_part * 100) / 60) # 1
minutes = round((decimal_part * 100) % 60) # 5
hours += extra_hours # 4
time_str = "%(hours)s:%(minutes)s" % {
"hours": hours,
"minutes": minutes
} # 4:5
final_time = datetime.datetime.strptime(time_str, "%H:%M").time()
print(final_time) # 04:05:00
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句