带小数点时间分钟转换
为什么要转换时间
由于业务所需,需要计算客户连接服务时长,一般情况连接服务时长都是小时的整数,比如1小时、3小时、8小时。但有的客户返回连接时长有分钟,比如A客户返回连接时长2小时30分钟,B客户返回连接时长为4小时45分钟。
做报表与客户沟通的客服就傻了,因为系统录入的时候是10进制的,(2小时30分钟就对应2.5小时, 4小时45分钟对应4.75小时)遇到小时的二分之一(半个小时)、四分之一(一刻钟)还会算,但她们遇到小时的非整除数有的人就不会计算了。
有一次一个客服问我,6小时18分钟对应小数点的小时是多少呢?
好吧,我给她们讲,用分钟除以60就可了。但有的人还是转不过弯,所以才有了时间转小数点的工具。
很简单的逻辑
用2小时25分钟作为说明数据
小时整数不用转换,整数为2
因为1小时=60分钟,所以25/60=0.4166666666666667
加起来就是2+0.417=2.417小时
反过来,同样的道理
2.32小时等于多少小时多少分钟呢?
2.32小时=2小时+0.32小时
2为小时整数
因为1小时=60分钟, 0.32小时*60=19.20分钟
加起来就是2小时19分钟
代码的实现
10进制与60进制的原理明白了,那代码就是English了
我们用Qt Designer 设计出界面
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>decToTimeclass> <widget class="QDialog" name="decToTime"> <property name="geometry"> <rect> <x>0x> <y>0y> <width>400width> <height>178height> rect> property> <property name="minimumSize"> <size> <width>400width> <height>0height> size> property> <property name="windowTitle"> <string>Dialogstring> property> <layout class="QHBoxLayout" name="horizontalLayout"> <item> <widget class="QGroupBox" name="groupBox"> <property name="title"> <string>分钟小数点转换string> property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QComboBox" name="comboBox"> <item> <property name="text"> <string>小数点转换为分钟string> property> item> <item> <property name="text"> <string>分钟转换为小数点string> property> item> widget> item> <item> <layout class="QHBoxLayout" name="horizontalLayout_3"> <item> <widget class="QLineEdit" name="input"/> item> <item> <widget class="QPushButton" name="button"> <property name="text"> <string>转换string> property> widget> item> layout> item> <item> <widget class="QTextEdit" name="text"/> item> layout> widget> item> layout> widget> <resources> <include location="qrc.qrc"/> resources> <connections/> ui>
用UIC转换为Py文件
python.exe -m PyQt5.uic.pyuic -o decimalToTime.py -x decimalToTime.ui
mian.py :
# -*- coding: utf-8 -*- from PyQt5 import QtCore, QtGui, QtWidgets from decimalToTime import Ui_decToTime import sys import icoqrc import re class MainFrom(QtWidgets.QDialog): def __init__(self): super(MainFrom, self).__init__() self.Ui = Ui_decToTime() self.Ui.setupUi(self) self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) # 设置总是在最前 self.setWindowIcon(QtGui.QIcon(':/ico/qq.ico')) self.setWindowFlags(QtCore.Qt.Dialog) # 设置类型为Dialog类型 self.Ui.button.clicked.connect(self.toTime) # 点击转换按钮 self.Ui.input.returnPressed.connect(self.toTime) # 回车事件 ''' 判断是否为数字 ''' def is_number(self, s): try: float(s) return True except ValueError: pass try: import unicodedata unicodedata.numeric(s) return True except (TypeError, ValueError): pass return False ''' '开始转换 ''' def toTime(self): # 判断input是否有值 # self.Ui.input.setText('2') pre = self.Ui.input.text() if self.is_number(pre) == False: OK = QtWidgets.QMessageBox.warning(self, (u'提示'), (u'请输入正确的数字!'), QtWidgets.QMessageBox.Yes) return False result = self.setText() self.Ui.text.setText(result) ''' 设置时间 ''' def setText(self): cindex = self.Ui.comboBox.currentIndex() # 当前comboBox索引Index值 索引从0开始 cindex = self.Ui.comboBox.currentText() # 当前comboBox文本值 r = re.compile(r'(\d+)\.(\d*)') m = r.match(self.Ui.input.text()) if cindex == '小数点转换为分钟': try: minute = str(int(float(str("0." + m.group(2))) * 60)) + "分钟" res = str(self.Ui.input.text()) + "小时等于" + str( m.group(1)) + "小时" + minute + "" except Exception as e: res = str(self.Ui.input.text()) + "小时等于" + str(self.Ui.input.text()) + "小时" if cindex == '分钟转换为小数点': try: point = int((float(m.group(2)) / 60) * 100) t = str(m.group(1)) + "." + str(point) res = str(m.group(1)) + "小时" + str(m.group(2)) + "分钟等于" + t + "小时" except Exception as e: res = str(self.Ui.input.text()) + "小时等于" + str(self.Ui.input.text()) + "小时" return res if __name__ == '__main__': App = QtWidgets.QApplication(sys.argv) MainApp = MainFrom() MainApp.show() sys.exit(App.exec_())
运行效果