【问题标题】:Python int() to the nearest valuePython int() 到最接近的值
【发布时间】:2015-10-07 17:49:53
【问题描述】:

pythonint()function 的工作方式是将数字四舍五入到最小值。

这样4.99 = 4

如果我想让 4.99 等于 5,我该怎么办?

【问题讨论】:

  • 如果你可以处理银行家的四舍五入,使用round
  • 谷歌“python四舍五入到最接近的整数”。发帖前有没有研究过?答案很容易找到

标签: python int


【解决方案1】:

int() 截断浮点值,删除数字的非整数部分。您正在寻找四舍五入

您可以为此使用round() function

>>> round(4.99)
5

【讨论】:

    【解决方案2】:

    有一个round() 内置:

    >>> round(4.99)
    5
    

    【讨论】:

      【解决方案3】:

      math.ceilmath.floor 返回浮点数,它们是参数上方或下方最接近的整数。

      【讨论】:

        猜你喜欢
        • 2016-07-22
        • 1970-01-01
        • 2012-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-20
        • 2015-03-01
        相关资源
        最近更新 更多