python打印浮点数,将时间输出转换为浮点数变量(Python)

本文介绍了一种使用Python进行时间计算的方法,并将其应用于工资结算的实际案例中。具体包括如何利用datetime模块处理输入的时间字符串,以及如何将计算出的工作时长转换为浮点数以便于后续的乘法运算。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值