用蟒蛇围捕 [英] rounding up with python
问题描述
我已阅读以下页面:
python 小数 - 舍入到最接近的整美元(无美分)- ROUND_HALF_UP
http://docs.python.org/library/decimal.html
我有以下代码:
total_num = Decimal(str(total/10))
total_num.quantize(Decimal('1'), rounding=ROUND_UP)
但它总是四舍五入?所以如果我有 221,我希望它返回 23.现在我得到 22.我对此有什么误解吗?
But its always rounding down? So if I have 221, I want it to return 23. Right now I'm getting 22. Is there something I'm misunderstanding regarding this?
我将其更改为以下内容:total_num = int(math.ceil(float(total)/10))
I changed it to the following: total_num = int(math.ceil(float(total)/10))
我需要一个 int
来继续具有 range
的 for
循环.
I needed an int
to continue with the for
loop that has a range
.
推荐答案
你不能使用 math.ceil 吗?如果您使用浮点除法,它将做必要的事情.目前你正在做一个整数除法.
Can't you use math.ceil? It will do the necessary thing if you are using a float division. Currently you are doing an integer division.
这篇关于用蟒蛇围捕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!