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 相关文章推荐
Python3实现连接SQLite数据库的方法
Aug 23 Python
Python中请使用isinstance()判断变量类型
Aug 25 Python
Python Tkinter GUI编程入门介绍
Mar 10 Python
详解Python的collections模块中的deque双端队列结构
Jul 07 Python
python微信跳一跳游戏辅助代码解析
Jan 29 Python
详解PyTorch批训练及优化器比较
Apr 28 Python
python 列表递归求和、计数、求最大元素的实例
Nov 28 Python
python配置grpc环境
Jan 01 Python
详解Python 中sys.stdin.readline()的用法
Sep 12 Python
Pandas-Cookbook 时间戳处理方式
Dec 07 Python
解决Python 写文件报错TypeError的问题
Oct 23 Python
Python paramiko使用方法代码汇总
Nov 20 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
上海地方志办公室-上海电子仪表工业志
2021/03/04 无线电
社区(php&&mysql)五
2006/10/09 PHP
建立文件交换功能的脚本(三)
2006/10/09 PHP
Laravel框架Request、Response及Session操作示例
2019/05/06 PHP
Js event事件在IE、FF兼容性问题
2011/01/01 Javascript
输入框的字数时时统计—关于 onpropertychange 和 oninput 使用
2011/10/21 Javascript
treepanel动态加载数据实现代码
2012/12/15 Javascript
JavaScript中的单引号和双引号报错的解决方法
2014/09/01 Javascript
jQuery对val和atrr("value")赋值的区别介绍
2014/09/26 Javascript
JS中的二叉树遍历详解
2016/03/18 Javascript
jQuey将序列化对象在前台显示地实现代码(方法总结)
2016/12/13 Javascript
Vue学习笔记进阶篇之vue-router安装及使用方法
2017/07/19 Javascript
小程序组件之仿微信通讯录的实现代码
2018/09/12 Javascript
Vue.js 中的 v-show 指令及用法详解
2018/11/19 Javascript
微信小程序下拉刷新PullDownRefresh的使用方法
2018/11/29 Javascript
JavaScript递归函数定义与用法实例分析
2019/01/24 Javascript
vue踩坑记-在项目中安装依赖模块npm install报错
2019/04/02 Javascript
JS Math对象与Math方法实例小结
2019/07/05 Javascript
微信小程序 扭蛋抽奖机css3动画实现详解
2019/07/19 Javascript
Vue实例的对象参数options的几个常用选项详解
2019/11/08 Javascript
vue动态设置页面title的方法实例
2020/08/23 Javascript
python实现在windows下操作word的方法
2015/04/28 Python
Python列表删除元素del、pop()和remove()的区别小结
2019/09/11 Python
Python单元测试与测试用例简析
2019/11/09 Python
pygame库实现移动底座弹球小游戏
2020/04/14 Python
Pytorch释放显存占用方式
2020/01/13 Python
python 爬虫如何正确的使用cookie
2020/10/27 Python
微软英国官方网站:Microsoft英国
2016/10/15 全球购物
美国用餐电影院:Alamo Drafthouse Cinema
2020/01/23 全球购物
Aosom西班牙:家具在线商店
2020/06/11 全球购物
const和static readonly区别
2013/05/20 面试题
创伤外科专业推荐信范文
2013/11/19 职场文书
大学生咖啡店创业计划书
2014/01/21 职场文书
小学教师求职信范文
2015/03/20 职场文书
2015年秋季开学典礼校长致辞
2015/07/16 职场文书
2016年中秋祝酒词
2015/11/26 职场文书