• Python 中的round函数


    在python2.7的doc中,round()的最后写着,

    "Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0." 

    保留值将保留到离上一位更近的一端(四舍六入),如果距离两端一样远,则保留到离0远的一边。所以round(0.5)会近似到1,而round(-0.5)会近似到-1。



    但是到了python3.5的doc中,文档变成了

    "values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice." 

    如果距离两边一样远,会保留到偶数的一边。比如round(0.5)和round(-0.5)都会保留到0,而round(1.5)会保留到2。

    Python 2.7 中的结果如下:

    >>> round(-0.5)
    -1.0
    >>> round(0.5)
    1.0
    >>> round(1.5)
    2.0
    >>> round(2.5)
    3.0
    >>> round(3.5)
    4.0
    >>> round(4.5)
    5.0
    >>> round(6.5)
    7.0

    Python 3.8 中的结果如下:

    round(-0.5)
    0
    round(0.5)
    0
    round(1.5)
    2
    round(2.5)
    2
    round(3.5)
    4
    round(4.5)
    4
    round(5.5)
    6
    round(6.5)
    6

  • 相关阅读:
    XML约束之DTD
    XML基础
    向方法传递多个参数
    从方法里返回值
    给方法传值
    对象状态和行为
    对象类型数组
    数组
    避开关键字
    primitive数据类型
  • 原文地址:https://www.cnblogs.com/emanlee/p/15189595.html
Copyright © 2020-2023  润新知