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 ORM框架SQLAlchemy学习笔记之数据添加和事务回滚介绍
Jun 10 Python
python实现给数组按片赋值的方法
Jul 28 Python
Python优先队列实现方法示例
Sep 21 Python
Python内置函数delattr的具体用法
Nov 23 Python
Python计算库numpy进行方差/标准方差/样本标准方差/协方差的计算
Dec 28 Python
浅谈pyqt5中信号与槽的认识
Feb 17 Python
python使用参数对嵌套字典进行取值的方法
Apr 26 Python
基于python实现模拟数据结构模型
Jun 12 Python
python根据用户需求输入想爬取的内容及页数爬取图片方法详解
Aug 03 Python
python基于opencv 实现图像时钟
Jan 04 Python
在 Golang 中实现 Cache::remember 方法详解
Mar 30 Python
Python下opencv使用hough变换检测直线与圆
Jun 18 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超级全局变量
2010/01/26 PHP
PHP中对用户身份认证实现两种方法
2011/06/04 PHP
zend optimizer在wamp的基础上安装图文教程
2013/10/26 PHP
PHP自定session保存路径及删除、注销与写入的方法
2014/11/18 PHP
Yii基于数组和对象的Model查询技巧实例详解
2015/12/28 PHP
thinkPHP数据查询常用方法总结【select,find,getField,query】
2017/03/15 PHP
PHP实践教程之过滤、验证、转义与密码详解
2017/07/24 PHP
Avengerls vs Newbee BO3 第一场2.18
2021/03/10 DOTA
Jquery AJAX 框架的使用方法
2009/11/03 Javascript
汉化英文版的Dreamweaver CS5并自动提示jquery
2010/11/25 Javascript
json的前台操作和后台操作实现代码
2012/01/20 Javascript
关于query Javascript CSS Selector engine
2013/04/12 Javascript
jQuery实现网页顶部固定导航效果代码
2015/12/24 Javascript
属于你的jQuery提示框(Tip)插件
2016/01/20 Javascript
javascript实现PC网页里的拖拽效果
2016/03/14 Javascript
JavaScript中push(),join() 函数 实例详解
2016/09/06 Javascript
浅谈jquery fullpage 插件增加头部和版权的方法
2018/03/20 jQuery
浅谈Webpack核心模块tapable解析
2018/09/11 Javascript
浅谈webpack性能榨汁机(打包速度优化)
2019/01/09 Javascript
详解js根据百度地图提供经纬度计算两点距离
2019/05/13 Javascript
微信小程序实现手势滑动卡片效果
2019/08/26 Javascript
浅谈JS中几种轻松处理'this'指向方式
2019/09/16 Javascript
使用webpack将ES6转化ES5的实现方法
2019/10/13 Javascript
MySQL中表的复制以及大型数据表的备份教程
2015/11/25 Python
Python Sql数据库增删改查操作简单封装
2016/04/18 Python
Flask之flask-script模块使用
2018/07/26 Python
Django连接数据库并实现读写分离过程解析
2019/11/13 Python
python 如何区分return和yield
2020/09/22 Python
python 输入字符串生成所有有效的IP地址(LeetCode 93号题)
2020/10/15 Python
家长会邀请书
2014/01/25 职场文书
高中军训感言800字
2014/03/05 职场文书
小学清明节活动方案
2014/03/08 职场文书
软件项目实施计划书
2014/05/02 职场文书
校长创先争优承诺书
2014/08/30 职场文书
文言文辞职信
2015/02/28 职场文书
2015年党建工作目标责任书
2015/05/08 职场文书