Basic python arithmetic - division

后端 未结 6 767
眼角桃花
眼角桃花 2021-01-05 14:40

I have two variables : count, which is a number of my filtered objects, and constant value per_page. I want to divide count by per_page and get integer value but I no matter

6条回答
  •  别那么骄傲
    2021-01-05 15:16

    Python does integer division when both operands are integers, meaning that 1 / 2 is basically "how many times does 2 go into 1", which is of course 0 times. To do what you want, convert one operand to a float: 1 / float(2) == 0.5, as you're expecting. And, of course, math.ceil(1 / float(2)) will yield 1, as you expect.

    (I think this division behavior changes in Python 3.)

提交回复
热议问题