Python中round函数
Python中round函数
- Python round()
- 描述
- 语法
- 参数
- 实例
- 补充
- round函数并不总是四舍五入
- 参数n
Python round()
描述
返回浮点数 x 的四舍五入值(其实时四舍六入,后文有讲原因)
语法
round(x[, n])
参数
x:数值表达式
n:数值表达式,表示精确到小数点位数,默认保留到整数位。
实例
#!/usr/bin/pythonprint("round(80.23456, 2) : ", round(80.23456, 2))
print("round(100.000056, 3) : ", round(100.000056, 3))
print("round(-100.000056, 3) : ", round(-100.000056, 3))
结果:
round(80.23456, 2) : 80.23
round(100.000056, 3) : 100.0
round(-100.000056, 3) : -100.0
补充
round函数并不总是四舍五入
如:
In [0]:round(2.355, 2)
Out [1]:2.35
因为该函数对于返回的浮点数并不是按照四舍五入的规则来计算,而会受到Python版本和计算机表示精度的影响。
1. Python2与Python3:
Python 2.7.8 (default, Oct 18 2021, 18:54:19)
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.5)
1.0
Python 3.4.3 (default, Oct 14 2021, 20:28:29)
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.5)
0
看一下Python文档:
Python2.7 round(),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.7 round(),文档变成了"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。
即round是四舍六入,但近似位为5时python2与python3结果不一样。所以如果有项目是从py2迁移到py3的,可要注意一下round的地方(当然,还要注意/和//,还有print,还有一些比较另类的库)。
计算机精度
>>> 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.ceil(天花板除法,向上取整),math.floor()(向下取整,int是截断,区别是负数时floor远离0,int靠近0)。
- python自带整除,python2中是/,3中是//,还有div函数。
- 字符串格式化可以做截断使用,例如 “%.2f” % value(保留两位小数并变成字符串……如果还想用浮点数请披上float()的外衣)。
- 当然,对浮点数精度要求如果很高的话,请用嘚瑟馍,不对不对,请用decimal模块。
参数n
当参数n不存在时,round()函数的输出为整数。
当参数n存在时,即使为0,round()函数的输出也会是一个浮点数。
此外,n的值可以是负数,表示在整数位部分四舍五入,但结果仍是浮点数。
例如:
print(round(123.45))
print(round(123.45,0))
print(round(123.45,-1))
结果是:
123
123.0
120.0
Python中round函数相关推荐
- python中round函数的精度保留方法---四舍六入五成双
当我们利用python进行数据计算时,通常会对浮点数保留相应的位数,这时候就会用到round函数,相信各位朋友在进行使用时会遇到各种问题,关于round函数保留精度.保留方法的问题,本文会进行详细的解 ...
- python中round函数参数_python中关于round函数的小坑
round函数很简单,对浮点数进行近似取值,保留几位小数.比如 >>> round(10.0/3, 2) 3.33 >>> round(20/7) 3 第一个参数是 ...
- python中round函数参数有负数_Python中round函数使用注意事项
使用round函数的时候发现了一个奇怪的现象.一直觉得round函数是一个用于四舍五入的函数,结果却不一定.一般如果觉得奇怪,那就是没弄懂其本质的运作原理,所以深入了解了下round函数. Pytho ...
- python中round函数的使用方法
python中使用的语法是round(number,digits) number,要四舍五入的数,digits是要小数点后保留的位数 如果 digits 大于 0,则四舍五入到指定的小数位 如果 di ...
- 【详解python中round函数】
在Python中,round()函数是一个内置函数,用于将一个数字四舍五入为指定的小数位数或整数位数. round()函数有两个参数:第一个参数是要四舍五入的数字,第二个参数(可选)是小数位数或整数位 ...
- 【问题解决】python中round()函数并非四舍五入
对于python中的round()函数,我们简单的认为是四舍五入,其实不然 对于偶数 >>> round(4.5) 4 对于奇数 >>> round(5.5) 6 ...
- python中round函数使用
round函数是一个用于四舍五入的函数: 在python3中,round(1.0/2.0)得到的是1,而在python2中,round(1.0/2.0)得到的是0 $ python Python 2. ...
- python中round函数_round函数
Python内置的round函数,用来取小数点后N位,还是很方便的.但是要注意,round函数执行的不是四舍五入.请参考:Python实现四舍五入的方法. 看代码吧,要注意round函数在去小数点后N ...
- python中 round函数的使用
round(参数,保留的小数位),该函数遵循四舍六入五成偶原则,被约的5前面的数值若为偶数时,则会舍弃5,若5前面的数值为奇数时则进一,若5后面有非0数值时则都会进一,round()中默认保留小数位是 ...
- python round函数_python中round函数如何使用
round函数很简单,对浮点数进行近似取值,保留几位小数.比如 >>> round(10.0/3, 2) 3.33 >>> round(20/7) 3 第一个参数是 ...
最新文章
- VTK:背景渐变BackgroundGradient用法实战
- java 18 -4 LinkedHashMap集合
- P1312 Mayan游戏 [模拟][搜索]
- 基于Http替补新闻WebService数据交换
- c++面向对象高级编程 学习一 不带指针的类
- 达梦数据库修改字段长度_解决达梦数据库新增大字段报错问题
- Linux常用命令笔记---创建私有YUM源
- c++ 十进制、十六进制和BCD的相互转换
- Flutter实战一Flutter聊天应用(十一)
- php 基础系列之 php快速入门
- android类之间的关系,Android 中Activity,Window和View之间的关系
- 【优化预测】基于matlab鲸鱼算法优化LSSVM预测【含Matlab源码 104期】
- 微波工程(第三版)中关于史密斯圆图部分总结
- 计算机桌面隔几秒闪一下,电脑最近怎么老是隔一段时间显示器就要闪一下
- 【工具推荐】SwitchHosts管理本地 Hosts
- 云计算在互联网发展史中的坐标
- 2023年2.14情人节最浪漫的表白烟花,送给自己的脑婆(源码)
- Springboot项目引入Bootstrap后,图标不能正常显示,报:Failed to decode downloaded font
- 根据epc和ra定位linux kernel panic或者应用程序的出错位置
- 选择 Delphi 2007 ( CodeGear Delphi 2007 for Win32 Version 11.0.2837.9583 ) 的理由