zoukankan      html  css  js  c++  java
  • python的round函数使用

    碰到的问题: 对float进行精确两位显示出来。 

    解决的方法:round(3.32342,2)  #3.32  .

    round函数概念:

    英文:圆,四舍五入
    是python内置函数,它在哪都能用,对数字取四舍五入。
    round(number[, ndigits])
    round 对传入的数据进行四舍五入,如果ngigits不传,默认是0(就是说保留整数部分).ngigits<0 的时候是来对整数部分进行四舍五入,返回的结果是浮点数.

    round 负数

    四舍五入是围绕着0来计算的,
    round(0.5)            # 1.0
    round(-0.5)         #-1.0

    round 的陷阱

    round(1.675, 2)    #1.68
    round(2.675, 2)    #2.67

    举例:

    round(3.4)         # 3.0
    round(3.5)         # 4.0
    round(3.6)         # 4.0
    round(3.6, 0)      # 4.0
    round(1.95583, 2)  # 1.96
    round(1241757, -3) # 1242000.0
    round(5.045, 2)    # 5.05
    round(5.055, 2)    # 5.06


    参考文档:http://docs.python.org/2.7/library/functions.html#round

  • 相关阅读:
    初识CC_MVPMatrix
    opengl启动过程
    http协议
    sockt套接字编程
    lua元表
    Codeforces 1203F1 Complete the Projects (easy version)
    CodeForces 1200E Compress Words
    CodeForces 1200D White Lines
    HDU 6656 Kejin Player
    HDU 6651 Final Exam
  • 原文地址:https://www.cnblogs.com/wanself/p/2763241.html
Copyright © 2011-2022 走看看