zoukankan      html  css  js  c++  java
  • (转)Python3的四舍五入round()函数坑爹?不,更科学!

    原文:https://blog.csdn.net/lly1122334/article/details/80596026

    Python3的四舍五入round()函数坑爹?不,更科学!
    Python2中,round()的结果就是我们所理解的四舍五入,round(1.5)=2,round(2.5)=3。
    Python3中,对round()函数有较大改动,例如round(1.5)=2,而round(2.5)却等于2,只有round(2.6)才等于3,这是为什么呢?

    原来Python2中的round()是四舍五入,而到了3,round()就变成了“四舍六入五成双”。
    这让我想起了大二时候的大物实验,第一节就讲了计数方法,其中印象最深刻的就是这个“四舍六入五成双”,它的作用是让统计数据更公平,降低舍入的误差。

    五成双的意思是,高位为单数则进1凑成双数,高位为双数则不进位。

    描述

    round() 方法返回浮点数x的四舍五入值。


    语法

    以下是 round() 方法的语法:

    round( x [, n]  )

    参数

    • x -- 数字表达式。
    • n -- 表示从小数点位数,其中 x 需要四舍五入,默认值为 0。

    返回值

    返回浮点数x的四舍五入值。


    实例

    以下展示了使用 round() 方法的实例:

    #!/usr/bin/python3
    
    print ("round(70.23456) : ", round(70.23456))
    print ("round(56.659,1) : ", round(56.659,1))
    print ("round(80.264, 2) : ", round(80.264, 2))
    print ("round(100.000056, 3) : ", round(100.000056, 3))
    print ("round(-100.000056, 3) : ", round(-100.000056, 3))

    以上实例运行后输出结果为:

    round(70.23456) :  70
    round(56.659,1) :  56.7
    round(80.264, 2) :  80.26
    round(100.000056, 3) :  100.0
    round(-100.000056, 3) :  -100.0
  • 相关阅读:
    jsp中添加弹窗口并且实现向后台双向传递数据
    hql中or的用法(代替union)
    hql中in的用法
    spring中的定时任务调度用例
    JS如何将UTC格式时间转本地格式
    HttpSession与Hibernate中Session的区别
    adaptive hash index
    InnoDB Double write
    int(M)与int
    MySQL库目录下db.opt文件的作用
  • 原文地址:https://www.cnblogs.com/liujiacai/p/10813311.html
Copyright © 2011-2022 走看看