Python内置函数(55)——round
英文文档:
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.1314926,1)
1.1
>>> round(1.1314926,5)
1.13149
2. ndigits参数为可选参数,当不传入时,即以默认保留0位小数进行取整,返回的是整数。
>>> round(1.1314926)
1
3. ndigits参数传入0时,虽然与不传入ndigits参数一样保留0位小数进行四舍五入,但是返回的值是浮点型。
>>> round(1.1314926,0)
1.0
4. ndigits参数小于0时,对整数部分进行四舍五入,ndigits参数控制了对浮点数的整数部分的后几位进行四舍五入,小数部分全部清0,返回类型是浮点数。如果传入的浮点数的整数部分位数小于ndigits参数绝对值,则返回0.0.
>>> round(11314.926,-1)
11310.0
>>> round(11314.926,-3)
11000.0
>>> round(11314.926,-4)
10000.0
>>> round(11314.926,-5)
0.0
5. round四舍五入时是遵循靠近0原则,所以-0.5和0.5进行0位四舍五入,返回的都是0.
>>> round(0.5)
0
>>> round(-0.5)
0
6. 对于浮点数求四舍五入有一个陷阱,有些四舍五入结果不像预期那样,比如round(2.675, 2) 的结果是2.67 而不是预期的 2.68,这不是bug,而是浮点数在存储的时候因为位数有限,实际存储的值和显示的值有一定误差。
>>> round(2.675, 2)
2.67
7. 对整数也能进行round操作,返回值也是整形。
>>> round(134567)
134567
>>> round(134567,0)
134567
>>> round(134567,1)
134567
>>> round(134567,2)
134567
>>> round(134567,-2)
134600
>>> round(134567,-6)
0
Python内置函数(55)——round的更多相关文章
- Python内置函数(6)——round
英文文档: round(number[, ndigits]) Return the floating point value number rounded to ndigits digits afte ...
- Python内置函数(55)——globals
英文文档: globals() Return a dictionary representing the current global symbol table. This is always the ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- Python入门之 Python内置函数
Python入门之 Python内置函数 函数就是以功能为导向,一个函数封装一个功能,那么Python将一些常用的功能(比如len)给我们封装成了一个一个的函数,供我们使用,他们不仅效率高(底层都是用 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- python 内置函数和函数装饰器
python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
随机推荐
- mybatis常用类起别名
在mybatis的配置文件中添加如下配置 <settings> <setting name="cacheEnabled" value="true&quo ...
- BP神经网络综合评价法
BP神经网络综合评价法是一种交互式的评价方法,一种既能避免人为计取权重的不精确性, 又能避免相关系数求解的复杂性,还能对数量较大且指标更多的实例进行综合评价的方法,它可以根据用户期望的输出不断修改指标 ...
- Candies POJ - 3159 (最短路+差分约束)
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...
- Linux-软中断通信
进程间通信-软中断 内容 使用软中段机制实现Linux进程间通信 机理说明 软中断信号(signal)是一种简单且最基本的进程通信机制,它最大的特点是提供了一种简单的处理异步事件的方法.例如,常见 ...
- 用js实现动态规划解决背包问题
动态规划的原理: 移至到该同学的博文中,讲解的声动易懂 https://www.jianshu.com/p/a66d5ce49df5 现在主要是用js来实现动态规划 function bb(v, w, ...
- windows本地用户及组的区别
Administrators(超级管理员组) 用来管理注册用户或者具有一定权限的其他管理员,维护网站的运行. Administrators中的用户对计算机/域有不受限制的完全访问权,分配给该组的默认权 ...
- C#调用Interrop.excel导出Excel文件失败解决方案
最近操作员反馈系统在导出Excel时失败,有抛出如下异常:系统错误信息:检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失 ...
- NOIP-接水问题
题目描述 学校里有一个水房,水房里一共装有m个龙头可供同学们打开水,每个龙头每秒钟的供水量相等,均为1. 现在有n名同学准备接水,他们的初始接水顺序已经确定.将这些同学按接水顺序从1到n编号,i号同学 ...
- 初学angular项目中遇到的一些问题
1.当angular渲染完成后操作DOM树方法 //当数据渲染完毕 ngApp.directive('repeatFinish', function () { return { ...
- Web版需求征集系统所得2,servlet中request.getParameter获值乱码问题解决
servlet获值乱码问题解决 解决办法一(最简单有效) request.setCharacterEncoding("utf-8"); 解决办法二 因为乱码问题的产生是因为默认格式 ...