Basic python arithmetic - division

后端 未结 6 800
眼角桃花
眼角桃花 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:39

    Integer division is the default of the / operator in Python < 3.0. This has behaviour that seems a little weird. It returns the dividend without a remainder.

    >>> 10 / 3
    3
    

    If you're running Python 2.6+, try:

    from __future__ import division
    
    >>> 10 / 3
    3.3333333333333335
    

    If you're running a lower version of Python than this, you will need to convert at least one of the numerator or denominator to a float:

    >>> 10 / float(3)
    3.3333333333333335
    

    Also, math.ceil always returns a float...

    >>> import math 
    >>> help(math.ceil)
    
    ceil(...)
        ceil(x)
    
        Return the ceiling of x as a float.
        This is the smallest integral value >= x.
    

提交回复
热议问题