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读取txt某几列绘图的方法
Oct 14 Python
python多进程控制学习小结
Oct 31 Python
Python机器学习之scikit-learn库中KNN算法的封装与使用方法
Dec 14 Python
Python删除n行后的其他行方法
Jan 28 Python
Python元组常见操作示例
Feb 19 Python
python里 super类的工作原理详解
Jun 19 Python
基于Python实现剪切板实时监控方法解析
Sep 11 Python
Python使用PyQt5/PySide2编写一个极简的音乐播放器功能
Feb 07 Python
pycharm激活码快速激活及使用步骤
Mar 12 Python
windows、linux下打包Python3程序详细方法
Mar 17 Python
利用Python实现字幕挂载(把字幕文件与视频合并)思路详解
Oct 21 Python
opencv用VS2013调试时用Image Watch插件查看图片
Jul 26 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中get_cfg_var()和ini_get()的用法及区别
2015/03/04 PHP
PHP调用.NET的WebService 简单实例
2015/03/27 PHP
JQuery Dialog的内存泄露问题解决方法
2010/06/18 Javascript
基于JQuery 滑动与动画的说明介绍
2013/04/18 Javascript
jquery.cookie实现的客户端购物车操作实例
2015/12/24 Javascript
浅谈Cookie的生命周期问题
2016/08/02 Javascript
JS中如何实现点击a标签返回页面顶部的问题
2017/01/19 Javascript
原生js实现选项卡功能
2017/03/08 Javascript
jQuery实现上传图片前预览效果功能
2017/08/03 jQuery
原生JS实现Ajax跨域请求flask响应内容
2017/10/24 Javascript
vue综合组件间的通信详解
2017/11/06 Javascript
原生JS进行前后端同构
2018/04/22 Javascript
vue侧边栏动态生成下级菜单的方法
2018/09/07 Javascript
angular6根据environments配置文件更改开发所需要的环境的方法
2019/03/06 Javascript
[04:29]2014DOTA2国际邀请赛 主赛事第三日TOPPLAY
2014/07/21 DOTA
[46:00]DOTA2上海特级锦标赛主赛事日 - 2 胜者组第一轮#4EG VS Fnatic第一局
2016/03/03 DOTA
一个简单的python程序实例(通讯录)
2013/11/29 Python
python关闭windows进程的方法
2015/04/18 Python
Python实现分割文件及合并文件的方法
2015/07/10 Python
python中的错误处理
2016/04/10 Python
Python的Flask框架中配置多个子域名的方法讲解
2016/06/07 Python
Python使用cookielib模块操作cookie的实例教程
2016/07/12 Python
python实现K最近邻算法
2018/01/29 Python
对Python正则匹配IP、Url、Mail的方法详解
2018/12/25 Python
Python基础知识点 初识Python.md
2019/05/14 Python
详解Python文件修改的两种方式
2019/08/22 Python
dpn网络的pytorch实现方式
2020/01/14 Python
python GUI库图形界面开发之pyinstaller打包python程序为exe安装文件
2020/02/26 Python
Python定义一个Actor任务
2020/07/29 Python
Fossil美国官网:化石手表、手袋、首饰及配饰
2019/02/17 全球购物
高分子材料与工程专业推荐信
2013/12/01 职场文书
冰淇淋店的创业计划书
2014/02/07 职场文书
中国梦演讲稿教师篇
2014/04/23 职场文书
《藏戏》教学反思
2016/02/23 职场文书
2019运动会广播加油稿汇总
2019/08/21 职场文书
nginx请求限制配置方法
2021/07/09 Servers