鱼C论坛

 找回密码
 立即注册
查看: 578|回复: 8

[已解决]为啥不能保留2位小数啊,求助

[复制链接]
发表于 2018-8-25 16:55:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在小甲鱼课后模块要求,提高精度,无法保留2位小数

  1. import time as t
  2. class Time():
  3.     def __init__(self):
  4.         self.prompt = "未开始计时!"
  5.         self.unit = ["年","月","日","小时","分钟","秒"]
  6.         self.lasted = []
  7.         self.begin = 0
  8.         self.begin_float =0
  9.         self.begin_int = 0
  10.         self.end_int =0
  11.         self.end = 0
  12.         self.end_float = 0

  13.     def __add__(self,other):
  14.         prompt = "总共运行了"
  15.         result = []      
  16.         for index in range(6):
  17.             result = self.lasted[index] + other.lasted[index]
  18.             if result[index]:
  19.                 prompt += str(result[index])+self.unit[index]
  20.                 return prompt


  21.     def __str__(self):
  22.         return self.prompt
  23.     __repr__ = __str__

  24.     #开始模块
  25.     def start(self):
  26.         [color=Red]self.begin_float = ((t.time()%1)*100//1)/100[/color]
  27.         self.begin_int = list(t.localtime())
  28.         self.begin_int[5] = self.begin_int[5] + self.begin_float
  29.         self.begin = self.begin_int[:]
  30.         self.prompt = "请先调用stop()终止计时器!"
  31.         print("正在计时...")

  32.     #结束模块
  33.     def stop(self):
  34.         if not self.begin:
  35.             print("请先调用begin()开始计时器")
  36.         else:
  37.             [color=Red]self.end_float = round(t.time()%1,2)[/color]
  38.             self.end_int = list(t.localtime())
  39.             self.end_int[5] = self.end_int[5] + self.end_float
  40.             self.end = self.end_int[:]
  41.             self._cacl()
  42.             print("计时结束!")

  43.     def _cacl(self):
  44.         self.lasted = []
  45.         self.prompt = "总共运行时间为:"
  46.         for index in range(6):
  47.             self.lasted.append(self.end[index]-self.begin[index])
  48.             if self.lasted[index]:
  49.                 self.prompt += str(self.lasted[index]) + self.unit[index]
  50.         #为下一轮初始化
  51.         self.begin = 0
  52.         self.begin_float =0
  53.         self.begin_int = 0
  54.         self.end_int =0
  55.         self.end = 0
  56.         self.end_float = 0
复制代码


尝试了2种方法,最后结果都是:
>>> t1=Time()
>>> t1.start()
正在计时...
>>> t1.stop()
计时结束!
>>> t1
总共运行时间为:2.3500000000000005秒
这是为什么?
最佳答案
2018-8-25 17:38:36
本帖最后由 JessiFly 于 2018-8-25 17:44 编辑
Chysial 发表于 2018-8-25 17:14
谢谢,我还想问一下,为什么会出现这种情况,能不能有直接的方法来修改小数位数?


第32行
  1. self.begin_int[5] = self.begin_int[5] + self.begin_float
复制代码

这里右边的self.begin_int[5]因为是int型,self.begin_float是float型,int和float相加,结果self.begin_int[5]是float。
第44行同理
最后得到的self.lasted[index]是两个float相减得到的,也是float。
而Python 3.X对于浮点数float默认的是提供17位数字的精度。(这里float有17位百度说的,我不确定具体默认多少位)
另外可以像3L那样写,毕竟年月日小时分钟有2位小数有点奇怪。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-25 17:04:02 | 显示全部楼层
格式化一下就行
第55行改为
  1. self.prompt += "%.2f" %(self.lasted[index]) + self.unit[index]
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-25 17:04:56 | 显示全部楼层
  1. import time as t
  2. class Time():
  3.     def __init__(self):
  4.         self.prompt = "未开始计时!"
  5.         self.unit = ["年","月","日","小时","分钟","秒"]
  6.         self.lasted = []
  7.         self.begin = 0
  8.         self.begin_float =0
  9.         self.begin_int = 0
  10.         self.end_int =0
  11.         self.end = 0
  12.         self.end_float = 0

  13.     def __add__(self,other):
  14.         prompt = "总共运行了"
  15.         result = []      
  16.         for index in range(6):
  17.             result = self.lasted[index] + other.lasted[index]
  18.             if result[index]:
  19.                 prompt += str(result[index])+self.unit[index]
  20.                 return prompt


  21.     def __str__(self):
  22.         return self.prompt
  23.     __repr__ = __str__

  24.     #开始模块
  25.     def start(self):
  26.         self.begin_float = ((t.time()%1)*100//1)/100
  27.         self.begin_int = list(t.localtime())
  28.         self.begin_int[5] = self.begin_int[5] + self.begin_float
  29.         self.begin = self.begin_int[:]
  30.         self.prompt = "请先调用stop()终止计时器!"
  31.         print("正在计时...")

  32.     #结束模块
  33.     def stop(self):
  34.         if not self.begin:
  35.             print("请先调用begin()开始计时器")
  36.         else:
  37.             self.end_float = round(t.time()%1,2)
  38.             self.end_int = list(t.localtime())
  39.             self.end_int[5] = self.end_int[5] + self.end_float
  40.             self.end = self.end_int[:]
  41.             self._cacl()
  42.             print("计时结束!")

  43.     def _cacl(self):
  44.         self.lasted = []
  45.         self.prompt = "总共运行时间为:"
  46.         for index in range(6):
  47.             self.lasted.append(self.end[index]-self.begin[index])
  48.             if self.lasted[index]:
  49.                 if index==5:
  50.                     self.prompt += '%.2f'%(self.lasted[index]) + self.unit[index]
  51.                 else:
  52.                     self.prompt += str(self.lasted[index]) + self.unit[index]
  53.         #为下一轮初始化
  54.         self.begin = 0
  55.         self.begin_float =0
  56.         self.begin_int = 0
  57.         self.end_int =0
  58.         self.end = 0
  59.         self.end_float = 0
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-25 17:14:03 | 显示全部楼层
JessiFly 发表于 2018-8-25 17:04
格式化一下就行
第55行改为

谢谢,我还想问一下,为什么会出现这种情况,能不能有直接的方法来修改小数位数?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-25 17:14:40 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-25 17:15:39 | 显示全部楼层

你好,米嘎
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-25 17:23:14 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-25 17:38:36 | 显示全部楼层    本楼为最佳答案   
本帖最后由 JessiFly 于 2018-8-25 17:44 编辑
Chysial 发表于 2018-8-25 17:14
谢谢,我还想问一下,为什么会出现这种情况,能不能有直接的方法来修改小数位数?


第32行
  1. self.begin_int[5] = self.begin_int[5] + self.begin_float
复制代码

这里右边的self.begin_int[5]因为是int型,self.begin_float是float型,int和float相加,结果self.begin_int[5]是float。
第44行同理
最后得到的self.lasted[index]是两个float相减得到的,也是float。
而Python 3.X对于浮点数float默认的是提供17位数字的精度。(这里float有17位百度说的,我不确定具体默认多少位)
另外可以像3L那样写,毕竟年月日小时分钟有2位小数有点奇怪。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-25 18:07:32 | 显示全部楼层
JessiFly 发表于 2018-8-25 17:38
第32行

这里右边的self.begin_int[5]因为是int型,self.begin_float是float型,int和float相加,结 ...

还是没太听懂,不过谢谢!以后有不会了,还请继续帮忙
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 15:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表