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 初始化多维数组代码
Sep 06 Python
python批量修改文件名的实现代码
Sep 01 Python
几个提升Python运行效率的方法之间的对比
Apr 03 Python
python定向爬取淘宝商品价格
Feb 27 Python
Python动态生成多维数组的方法示例
Aug 09 Python
python 重命名轴索引的方法
Nov 10 Python
对python pandas读取剪贴板内容的方法详解
Jan 24 Python
Python操作配置文件ini的三种方法讲解
Feb 22 Python
基于Django静态资源部署404的解决方法
Jul 28 Python
简单了解Pandas缺失值处理方法
Nov 16 Python
Python过滤序列元素的方法
Jul 31 Python
通过代码实例了解Python sys模块
Sep 14 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
PHP新手上路(五)
2006/10/09 PHP
PHP学习笔记之数组篇
2011/06/28 PHP
Laravel 自动生成验证的实例讲解:login / logout
2019/10/14 PHP
javascript 对象的定义方法
2007/01/10 Javascript
图片连续滚动代码[兼容IE/firefox]
2009/06/11 Javascript
jquery BS,dialog控件自适应大小
2009/07/06 Javascript
图片轮换效果实现代码(点击按钮停止执行)
2013/04/12 Javascript
js仿百度有啊通栏展示效果实现代码
2013/05/28 Javascript
左侧是表头的JS表格控件(自写,网上没有的)
2013/06/04 Javascript
封装了jQuery的Ajax请求全局配置
2015/02/05 Javascript
jquery背景跟随鼠标滑动导航
2015/11/20 Javascript
全面解析Bootstrap中Carousel轮播的使用方法
2016/06/13 Javascript
Express URL跳转(重定向)的实现方法
2017/04/07 Javascript
vue.js源代码core scedule.js学习笔记
2017/07/03 Javascript
jquery实现限制textarea输入字数的方法
2017/09/06 jQuery
微信禁止下拉查看URL的处理方法
2017/09/28 Javascript
基于js中style.width与offsetWidth的区别(详解)
2017/11/12 Javascript
JavaScript的词法结构精华篇
2018/10/17 Javascript
Express结合Webpack的全栈自动刷新
2019/05/23 Javascript
vue中获取滚动table的可视页面宽度调整表头与列对齐(每列宽度不都相同)
2019/08/17 Javascript
layui实现数据表格隐藏列的示例
2019/10/25 Javascript
Python操作MySQL数据库9个实用实例
2015/12/11 Python
Python 打印中文字符的三种方法
2018/08/14 Python
详解Python 爬取13个旅游城市,告诉你五一大家最爱去哪玩?
2019/05/07 Python
python三大神器之fabric使用教程
2019/06/10 Python
自适应线性神经网络Adaline的python实现详解
2019/09/30 Python
美体小铺奥地利官方网站:The Body Shop奥地利
2019/04/11 全球购物
anello泰国官方网站:日本流行包包品牌
2019/08/08 全球购物
机电专业体育教师求职信
2013/09/21 职场文书
双十佳事迹材料
2014/01/29 职场文书
大学生英语演讲稿
2014/04/24 职场文书
2015年企业团支部工作总结
2015/05/21 职场文书
住房公积金贷款工资证明
2015/06/12 职场文书
药房管理制度范本
2015/08/06 职场文书
win10+anaconda安装yolov5的方法及问题解决方案
2021/04/29 Python
Linux服务器离线安装 nginx的详细步骤
2022/06/16 Servers