python中round函数如何使用


Posted in Python onJune 19, 2020

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)

如果我们阅读一下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()的外衣)。

当然,对浮点数精度要求如果很高的话,请用?N瑟馍,不对不对,请用decimal模块。

内容扩展:

round(number,num_digits)

Number 需要进行四舍五入的数字。

Num_digits 指定的位数,按此位数进行四舍五入。

注解

  • 如果 num_digits 大于 0,则四舍五入到指定的小数位。
  • 如果 num_digits 等于 0,则四舍五入到最接近的整数。
  • 如果 num_digits 小于 0,则在小数点左侧进行四舍五入。

示例

x=1.343671234
print x
print round(x,1)
print round(x,2)
print round(x,3)

输出结果为:

1.343671234
1.3
1.34
1.344

到此这篇关于python中round函数如何使用的文章就介绍到这了,更多相关python的round函数用法总结内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python模块包中__init__.py文件功能分析
Jun 14 Python
详谈套接字中SO_REUSEPORT和SO_REUSEADDR的区别
Apr 28 Python
Python爬虫框架Scrapy基本用法入门教程
Jul 26 Python
Python 使用 attrs 和 cattrs 实现面向对象编程的实践
Jun 12 Python
python从list列表中选出一个数和其对应的坐标方法
Jul 20 Python
Python 等分切分数据及规则命名的实例代码
Aug 16 Python
python3实现微型的web服务器
Sep 03 Python
Python values()与itervalues()的用法详解
Nov 27 Python
Python图片的横坐标汉字实例
Dec 04 Python
python 多线程共享全局变量的优劣
Sep 24 Python
Python经纬度坐标转换为距离及角度的实现
Nov 01 Python
python爬虫selenium模块详解
Mar 30 Python
keras实现theano和tensorflow训练的模型相互转换
Jun 19 #Python
Keras 切换后端方式(Theano和TensorFlow)
Jun 19 #Python
python中怎么表示空值
Jun 19 #Python
Python调用OpenCV实现图像平滑代码实例
Jun 19 #Python
使用OpenCV对车道进行实时检测的实现示例代码
Jun 19 #Python
为什么python比较流行
Jun 19 #Python
查看keras的默认backend实现方式
Jun 19 #Python
You might like
雄兵连:天使彦天使彦为爱折翼,彦和炙心同时念动的誓言!
2020/03/02 国漫
按上下级层次关系输出内容的PHP代码
2010/07/17 PHP
Views rows style模板重写代码
2011/05/16 PHP
PHP 中检查或过滤IP地址的实现代码
2011/11/27 PHP
PHP登录环节防止sql注入的方法浅析
2014/06/30 PHP
PHP提示Cannot modify header information - headers already sent by解决方法
2014/09/22 PHP
window.open关于浏览器拦截问题分析及解决方法
2013/02/05 Javascript
jQuery jcrop插件截图使用方法
2013/11/20 Javascript
JS window对象的top、parent、opener含义介绍
2013/12/03 Javascript
验证控件与Button的OnClientClick事件详细解析
2013/12/04 Javascript
基于ajax实现文件上传并显示进度条
2015/08/03 Javascript
基于jQuery全屏焦点图左右切换插件responsiveslides
2015/09/07 Javascript
详解JavaScript对象序列化
2016/01/19 Javascript
JS继承之借用构造函数继承和组合继承
2016/09/07 Javascript
用jquery的attr方法实现图片切换效果
2017/02/05 Javascript
bootstrap轮播图示例代码分享
2017/05/17 Javascript
详解Webstorm 新建.vue文件支持高亮vue语法和es6语法
2017/10/26 Javascript
JavaScript数据结构之优先队列与循环队列实例详解
2017/10/27 Javascript
bootstrap fileinput插件实现预览上传照片功能
2018/01/23 Javascript
JS基于对象的链表实现与使用方法示例
2019/01/31 Javascript
详解如何写出一个利于扩展的vue路由配置
2019/05/16 Javascript
python中range()与xrange()用法分析
2016/09/21 Python
Python基于递归算法实现的走迷宫问题
2017/08/04 Python
python编程培训 python培训靠谱吗
2018/01/17 Python
python matlibplot绘制3D图形
2018/07/02 Python
利用python和ffmpeg 批量将其他图片转换为.yuv格式的方法
2019/01/08 Python
python实现BP神经网络回归预测模型
2019/08/09 Python
HTML5中使用postMessage实现两个网页间传递数据
2016/06/22 HTML / CSS
社区工作者演讲稿
2014/05/23 职场文书
2014年城管工作总结
2014/11/20 职场文书
给上级领导的感谢信
2015/01/22 职场文书
职工培训工作总结
2015/08/10 职场文书
领导莅临指导欢迎词
2015/09/30 职场文书
2019XX公司员工考核管理制度!
2019/08/07 职场文书
Python基础之赋值,浅拷贝,深拷贝的区别
2021/04/30 Python
react国际化react-intl的使用
2021/05/06 Javascript