返回信息流Question 1:四舍五入到最接近0.05
比如,7.125四舍五入,结果是7.15
我找到math库里面的ceil()函数:
ceil(...)
ceil(x)
Return the ceiling of x as a float.
This is the smallest integral value >= x.
>>>math.ceil(3.14)
4.0
还有BIF的round():
round(...)
round(number[, ndigits]) -> floating point number
Round a number to a given precision in decimal digits (default 0 digits).
This always returns a floating point number. Precision may be negative.
>>>round(13.344,2) #第二个参数是精确到第几位小数
13.34
我是Python新手,求大神指点。看看有没有办法从ceil()或者round()变形一下,或者有没有别的函数可以实现?
这是一条镜像帖。来源:北邮人论坛 / python / #6191同步于 2015/4/10
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖
向上舍入到最接近0.05,用什么函数?
WinU
2015/4/10镜像同步12 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
多谢!
2.刚也想到正则表达式了
re.match(r"\d+\.\d+",string)
1.这个的话,暂时是这个结果,是不是要改改参数什么的呢?
>>>round(1.715*20)/20
1.7
【 在 nuanyangyang 的大作中提到: 】
: 1.先乘以20然后round最后除以20
: 2.用正则表达式吧
【 在 WinU 的大作中提到: 】
: 多谢!
: 2.刚也想到正则表达式了
: re.match(r"\d+\.\d+",string)
: ...................
1.7很对啊,怎么了?
对对,很对。我看错了。
>>>round(7.125*20)/20
7.15
这就是想要的结果,多谢!
【 在 nuanyangyang 的大作中提到: 】
:
: 1.7很对啊,怎么了?
哈,我是说穷举= =。。。
【 在 WinU 的大作中提到: 】
: 对对,很对。我看错了。
: >>>round(7.125*20)/20
: 7.15
: ...................
来自「北邮人论坛手机版」
更新:
求11.25*0.05:
11.25*0.05=0.5625,向上舍入最接近的0.05为0.60
但是:
>>>round(11.25*0.05*20,2)/20
0.55
不符合要求。
改进措施:round和ceil结合一起用
>>>ceil(round(11.25*0.05*20,2))/20
0.60