英文文档:

round(number[, ndigits])
Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it returns the nearest integer to its input. Delegates to number.__round__(ndigits).
For the built-in types supporting round(), 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 (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). The return value is an integer if called with one argument, otherwise of the same type as number.
Note

The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.

说明:

1.round 函数用于对浮点数进行四舍五入的求值,具体保留几位小数,以出阿如的 ndigits 参数来控制
>>> round(1.423432432)
1
>>>
>>> round(1.423432432,2)
1.42
>>> round(1.423432432,5)
1.42343
>>>
>>>
>>> round(1.423432432)
1
>>>

  2. ndigits参数为可选参数,当不传入时,即以默认保留0位小数进行取整,返回的是整数。

>>> round(1.1314926)
1  
 3.当传入的值是0时与不传值一样保留0位小数,但是是浮点类型的数
>>> round(1.423432432,0)
1.0
>>>

  4.也可以传入负数,则标示是对整数部分进行四舍五入,小数部分全部清0,若果传入的 ndigits 值大于浮点数的整数部分,则返回0.0

>>> round(1314.423432432,-2)
1300.0
>>> round(1314.423432432,-4)
0.0
>>>

  5.round 四舍五入是靠近0原则,所以-0.5和0.5进行0位的四舍五入结果都是0,

>>> round(0.5)
0
>>> round(-0.5)
0
>>>

  6.round 有时会有 bug,因为浮点数在存储的时候因为数位有限,存储的不精却,存储的值和实际显示的值有误差,导致四舍五入的时候也不精确。

>>> round(2.2458,3)
2.246
>>> round(2.245,3)
2.245
>>> round(2.245,2)
2.25
>>> round(2.675,2)
2.67
>>> round(2.674545,4)
2.6745
>>>
>>>

  7.round 对整数也能进行操作,但会也是整形。

>>>
>>> round(123)
123
>>> round(123,2)
123
>>> round(123,-2)
100
>>> round(123,-3)
0
>>>





最新文章

  1. Oracle 12c In Memory Option初探
  2. Objective-C实现发短信和接电话
  3. namenode metadata 备份与恢复实验
  4. php微信接口实例
  5. git学习相关资料
  6. Linux 下搭建ftp服务器 指定用户指定目录及其他操作
  7. JBoss 系列六十九:CDI 基本概念
  8. HDU 2121 Ice_cream’s world II 最小树形图 模板
  9. BottomupSort算法 c++代码实现
  10. 调试和运行matlab代码(源程序)的技巧和教程
  11. Python 爬虫 爬校花网!!
  12. Netty之心跳检测技术(四)
  13. 笔记:JDBC 数据库
  14. python之面向对象的程序设计
  15. Python3基础系列-基本入门语法
  16. 多线程局部变量之threading.local()用法
  17. 【转】“菜”鸟理解.NET Framework(CLI,CLS,CTS,CLR,FCL,BCL)
  18. Python3.x:关于urllib中urlopen报错问题的解决方案
  19. springboot集成thymeleaf中遇到的问题
  20. 继续ajax长轮询解决方案--递归

热门文章

  1. 关于如何使用SVN的一些建议
  2. java 文本图片字体模糊优化处理
  3. iOS加密算法总结
  4. Sublime + Python3 + 虚拟环境 + 去除 中文输出乱码
  5. 归并排序及优化(Java实现)
  6. 1-6 hibernate映射集合属性
  7. [Bzoj 2547] [Ctsc2002] 玩具兵
  8. 数据段、代码段、堆栈段、BSS段的区别
  9. 笔记:XML-解析文档
  10. js先后对某个js对象内的两个属性排序