Python内置函数(6)——round
2024-08-24 06:07:02
英文文档:
round
(number[, ndigits])- Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it returns the nearest integer to its input. Delegates to
number.__round__(ndigits)
. - For the built-in types supporting
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 (so, for example, bothround(0.5)
andround(-0.5)
are0
, andround(1.5)
is2
). The return value is an integer if called with one argument, otherwise of the same type as number. - 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.
说明:
1.round 函数用于对浮点数进行四舍五入的求值,具体保留几位小数,以出阿如的 ndigits 参数来控制
>>> round(1.423432432)
1
>>>
>>> round(1.423432432,2)
1.42
>>> round(1.423432432,5)
1.42343
>>>
>>>
>>> round(1.423432432)
1
>>>
2. ndigits参数为可选参数,当不传入时,即以默认保留0位小数进行取整,返回的是整数。
>>> round(1.1314926)
1
3.当传入的值是0时与不传值一样保留0位小数,但是是浮点类型的数
>>> round(1.423432432,0)
1.0
>>>
4.也可以传入负数,则标示是对整数部分进行四舍五入,小数部分全部清0,若果传入的 ndigits 值大于浮点数的整数部分,则返回0.0
>>> round(1314.423432432,-2)
1300.0
>>> round(1314.423432432,-4)
0.0
>>>
5.round 四舍五入是靠近0原则,所以-0.5和0.5进行0位的四舍五入结果都是0,
>>> round(0.5)
0
>>> round(-0.5)
0
>>>
6.round 有时会有 bug,因为浮点数在存储的时候因为数位有限,存储的不精却,存储的值和实际显示的值有误差,导致四舍五入的时候也不精确。
>>> round(2.2458,3)
2.246
>>> round(2.245,3)
2.245
>>> round(2.245,2)
2.25
>>> round(2.675,2)
2.67
>>> round(2.674545,4)
2.6745
>>>
>>>
7.round 对整数也能进行操作,但会也是整形。
>>>
>>> round(123)
123
>>> round(123,2)
123
>>> round(123,-2)
100
>>> round(123,-3)
0
>>>
最新文章
- Oracle 12c In Memory Option初探
- Objective-C实现发短信和接电话
- namenode metadata 备份与恢复实验
- php微信接口实例
- git学习相关资料
- Linux 下搭建ftp服务器 指定用户指定目录及其他操作
- JBoss 系列六十九:CDI 基本概念
- HDU 2121 Ice_cream’s world II 最小树形图 模板
- BottomupSort算法 c++代码实现
- 调试和运行matlab代码(源程序)的技巧和教程
- Python 爬虫 爬校花网!!
- Netty之心跳检测技术(四)
- 笔记:JDBC 数据库
- python之面向对象的程序设计
- Python3基础系列-基本入门语法
- 多线程局部变量之threading.local()用法
- 【转】“菜”鸟理解.NET Framework(CLI,CLS,CTS,CLR,FCL,BCL)
- Python3.x:关于urllib中urlopen报错问题的解决方案
- springboot集成thymeleaf中遇到的问题
- 继续ajax长轮询解决方案--递归