你如何在Python中整理一个数字? [英] How do you round UP a number in Python?
本文介绍了你如何在Python中整理一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了一个循环(数字),但是它将数字向下舍入。例如:
round(2.3)= 2.0而不是3,我想要
我尝试了int(数字+ .5),但是它再次围绕数字!例子:
int(2.3 + .5)= 2
然后我试了一下(数字+5),但是它在边缘情况下不起作用。示例:
等待!这工作!
请指教
解决方案
ceil (天花板)功能:
$ p $ 导入数学
print math.ceil(4.2)
This problem is killing me. How does one roundup a number UP in Python?
I tried round(number) but it round the number down. Example:
round(2.3) = 2.0 and not 3, what I would like
The I tried int(number + .5) but it round the number down again! Example:
int(2.3 + .5) = 2
Then I tried round(number + .5) but it won't work in edge cases. Example:
WAIT! THIS WORKED!
Please advise.
解决方案
The ceil (ceiling) function:
import math
print math.ceil(4.2)
这篇关于你如何在Python中整理一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文