How to perform ceiling-division in integer arithmetic? [duplicate]

核能气质少年 提交于 2019-12-01 16:54:25

For your use case, use integer arithmetic. There is a simple technique for converting integer floor division into ceiling division:

items = 102
boxsize = 10
num_boxes = (items + boxsize - 1) // boxsize

Alternatively, use negation to convert floor division to ceiling division:

num_boxes = -(items // -boxsize)

Negate before and after?

>>> -(-102 // 10)
11
from math import ceil

print(ceil(10.3))

11

You can try :

import math
math.ceil( x )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!