python中关于round函数的小坑
这个一直都想写,但是因为这个点比较小,所以一直懒得动手。不过还是补上吧,留着早晚是个祸害。
round函数很简单,对浮点数进行近似取值,保留几位小数。比如
>>> round(10.0/3, 2)
3.33
>>> round(20/7)
3
第一个参数是一个浮点数,第二个参数是保留的小数位数,可选,如果不写的话默认保留到整数。
这么简单的函数,能有什么坑呢?
1、round的结果跟python版本有关
我们来看看python2和python3中有什么不同:
$ python
Python 2.7.8 (default, Jun 18 2015, 18:54:19)
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.5)
1.0
$ python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.5)
0
好玩吗?
如果我们阅读一下python的文档,里面是这么写的:
在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。
所以如果有项目是从py2迁移到py3的,可要注意一下round的地方(当然,还要注意/和//,还有print,还有一些比较另类的库)。
2、特殊数字round出来的结果可能未必是想要的。
>>> round(2.675, 2)
2.67
python2和python3的doc中都举了个相同的栗子,原文是这么说的:
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.
简单的说就是,round(2.675, 2) 的结果,不论我们从python2还是3来看,结果都应该是2.68的,结果它偏偏是2.67,为什么?这跟浮点数的精度有关。我们知道在机器中浮点数不一定能精确表达,因为换算成一串1和0后可能是无限位数的,机器已经做出了截断处理。那么在机器中保存的2.675这个数字就比实际数字要小那么一点点。这一点点就导致了它离2.67要更近一点点,所以保留两位小数时就近似到了2.67。
以上。除非对精确度没什么要求,否则尽量避开用round()函数。近似计算我们还有其他的选择:
- 使用math模块中的一些函数,比如math.ceiling(天花板除法)。
- python自带整除,python2中是/,3中是//,还有div函数。
- 字符串格式化可以做截断使用,例如 "%.2f" % value(保留两位小数并变成字符串……如果还想用浮点数请披上float()的外衣)。
- 当然,对浮点数精度要求如果很高的话,请用嘚瑟馍,不对不对,请用decimal模块。
就酱。

python中关于round函数的小坑的更多相关文章
- Python 中关于 round 函数的小坑
参考: http://www.runoob.com/w3cnote/python-round-func-note.html
- Python 中关于 round 函数的坑
round函数很简单(而且不需要引入math模块),对浮点数进行近似取值,保留几位小数. 比如 # -*- coding: UTF-8 -*- r1=round(12.12345,3) r2=roun ...
- Python中需要注意的一些小坑
Python小知识 # a = a + b /a += b 有时是不一样的 a=[1,2,3] b = a a = a + [4,5,6] # a=[1,2,3] # b = a # a += ...
- Jquery中on绑定的一些小坑
---恢复内容开始--- 今天我们来说说关于JQuery中事件绑定中on绑定的一些小问题,直接上代码了,大家拷下去就可以用 <!DOCTYPE html> <html lang=&q ...
- 【转载】Sqlserver中使用Round函数对计算结果四舍五入
在实际应用的计算中,很多时候我们需要对最后计算结果四舍五入,其实在Sqlserver中也有对应的四舍五入函数,就是Round函数,Round函数的格式为Round(column_name,decima ...
- python --- Python中的callable 函数
python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...
- python中使用zip函数出现<zip object at 0x02A9E418>
在Python中使用zip函数,出现<zip object at 0x02A9E418>错误的原因是,你是用的是python2点多的版本,python3.0对python做了改动 zip方 ...
- [转载]python中multiprocessing.pool函数介绍
原文地址:http://blog.sina.com.cn/s/blog_5fa432b40101kwpi.html 作者:龙峰 摘自:http://hi.baidu.com/xjtukanif/blo ...
- Python 中的isinstance函数
解释: Python 中的isinstance函数,isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo) 如果参数object是cla ...
随机推荐
- 061 hive中的三种join与数据倾斜
一:hive中的三种join 1.map join 应用场景:小表join大表 一:设置mapjoin的方式: )如果有一张表是小表,小表将自动执行map join. 默认是true. <pro ...
- 桌面版centos安装vncserver并在windows下使用VNC Viewer远程连接
首先关闭防火墙 在Centos中安装vncserver yum install tigervnc-server 拷贝一份 /lib/systemd/system/vncserver@.service ...
- Openvas安装
Openvas简介 Openvas是开源的,是Nessus项目分支,用于管理目标系统的漏洞,检测目标网络或主机的安全性.它的评估能力来源于数万个漏洞测试程序,openvas 早起版本还有一个客户端,现 ...
- Linux 运行Python文件,不因终端关闭而终止运行
在Linux服务器运行py文件时,有时会因为终端窗口的关闭而结束py文件的执行,这时候使用下面的命令运行py文件: $nohup python filename.py & 命令解释: nohu ...
- Python数据可视化系列-01-快速绘图
快速绘图 数据图绘制 matplotlib的字库pyplot提供了快速绘制2D图标的API接口. import numpy as np import matplotlib.pyplot as plt ...
- Struts2返回json数据xml中配置
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-/ ...
- JavaScript_几种继承方式(2017-07-04)
原型链继承 核心: 将父类的实例作为子类的原型 //父类 function SuperType() { this.property = true; } SuperType.prototype.ge ...
- Centos 7 安装 Mysql 5.5 5.6 5.7
环境 [root@node1 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@node1 ~]# uname -a Linu ...
- unity DoTween使用
先说插件获取,DoTween是一个开源的插件,它的代码托管在Github上[https://github.com/Demigiant/dotween].若只是单纯项目需要是可以去AssetStore获 ...
- C#编程(八十)---------- 异常类
异常类 在C#里,异常处理就是C#为处理错误情况提供的一种机制.它为每种错误情况提供了定制的处理方式,并且把标志错误的代码预处理错误的代码分离开来. 对.net类来说,一般的异常类System.Exc ...