本文介绍了将时间输出转换为浮点数变量(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Tkinter导入的$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b $ 35.34)
amp = 2.40
pmp = 2.50



signOnSun1 = raw_input(星期日你在什么时间登录);
signOffSun1 = raw_input(星期日你星期天离开什么时间);

if(signOnSun1< 0600):
rate1 = rate1 + amp
elif(signOnSun1> 1100):
rate1 = rate1 + pmp
elif(signOffSun1> 1800):
rate1 = rate1 + pmp
else:
rate1 = 35.34

FMT ='%H%M'
timeWorkedSun1 =((datetime.strptime(signOffSun1,FMT)) - (datetime.strptime(signOnSun1,FMT)))

*我想将timeWorkedSun1转换为一个float,所以我可以将其与rate1相乘,但似乎是我生命中的祸根。任何想法?*

解决方案

timeWorkedSun1 是类型 datetime.timedelta 。调用它的 total_seconds 方法将其转换为第二个数值(然后除以3600,以小时计)。即:

  time_mul_by_hourly_rate =(timeWorkedSun1.total_seconds()/ 3600.0)* rate1 


from Tkinter import *
from datetime import datetime
from datetime import timedelta
import math

rate1 = str(35.34)
amp = 2.40
pmp = 2.50



signOnSun1 = raw_input("What time did you sign on Sunday: ");
signOffSun1 = raw_input("What time did you sign off Sunday: ");

if (signOnSun1 < 0600):
    rate1 = rate1 + amp
elif (signOnSun1 > 1100):
    rate1 = rate1 + pmp
elif (signOffSun1 > 1800):
    rate1 = rate1 + pmp
else:
    rate1 = 35.34

FMT = '%H%M'
timeWorkedSun1 =  ( (datetime.strptime(signOffSun1, FMT)) -  (datetime.strptime   (signOnSun1, FMT)))

* I want to convert timeWorkedSun1 to a float so i can multiply it with rate1 but it seems to be the bane of my life. Any ideas?*

解决方案

timeWorkedSun1 is of type datetime.timedelta. Call its total_seconds method to translate it into a number of second (then divide by 3600 to get it in hours-and-fractions). I.e:

time_mul_by_hourly_rate = (timeWorkedSun1.total_seconds() / 3600.0) * rate1

这篇关于将时间输出转换为浮点数变量(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:33