V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
kenneth
V2EX  ›  Python

Python除法如何向上取整?

  •  
  •   kenneth · Dec 30, 2012 · 22411 views
    This topic created in 4879 days ago, the information mentioned may be changed or developed.
    求思路。
    11 replies    1970-01-01 08:00:00 +08:00
    clowwindy
        1
    clowwindy  
       Dec 30, 2012
    >>> round(2.5)
    3.0

    指这个?
    clowwindy
        2
    clowwindy  
       Dec 30, 2012
    >>> math.ceil(2.1)
    3.0
    kenneth
        3
    kenneth  
    OP
       Dec 30, 2012
    @clowwindy 我是说python除法的向上取整,你的是向上取整,但是不是python除法。
    darcy
        4
    darcy  
       Dec 30, 2012 via iPhone   ❤️ 6
    UP(A/B) = int((A+B-1)/B)
    楼主要的这个?
    funagi
        5
    funagi  
       Dec 30, 2012
    reusFork
        6
    reusFork  
       Dec 30, 2012 via Android
    math.ceil(10 / 3.0)
    sNullp
        7
    sNullp  
       Dec 30, 2012
    @reusFork 楼主两个数字都是整型。4楼是正解。
    reusFork
        8
    reusFork  
       Dec 30, 2012
    @sNullp 这个公式确实方便,我之前都是用math.ceil(A / float(B))
    yegle
        9
    yegle  
       Dec 31, 2012 via iPad
    看了半天才明白…

    from __future__ import division
    result = math.ceil(3/2)

    最简单明了了
    reloop
        10
    reloop  
       Dec 6, 2013
    @darcy
    @sNullp
    @reusFork
    请问为什么 UP(A/B) = int( (A+B-1)/B ) 可以向上取整? 计算过程是怎样的?
    sNullp
        11
    sNullp  
       Dec 6, 2013
    计算过程就是 A 加 B 减 1 除 B。。
    你拿纸算一下 A+B-1 的范围就知道了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2977 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 12:12 · PVG 20:12 · LAX 05:12 · JFK 08:12
    ♥ Do have faith in what you're doing.