【问题标题】:controlling if the number is being rounded up or down in python控制数字在python中是向上还是向下舍入
【发布时间】:2013-07-05 18:20:46
【问题描述】:

所以我知道如何在 python 中对数字进行四舍五入。这很简单。 但我想知道有没有办法将随机浮点数舍入到下一个整数。 或者直接向下圆。 因此,如果有 8.7 可以选择将其降低到 8。 或者如果有 8.3 可以选择将其提升到 9。

因为这个数字是一个随机浮点数,我不知道它是 8.3 还是 8.7 还是 8.5 还是 8.48237,但我总是希望它四舍五入到 9。这可能吗?

【问题讨论】:

    标签: python python-2.7 python-3.x floating-point rounding


    【解决方案1】:

    您一定是在寻找math.ceil

    >>> from math import ceil
    >>> ceil(8.3)
    9.0
    >>> ceil(8.48237)
    9.0
    

    ceil函数->

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 2011-12-26
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多