Python内置函数(6)——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.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
>>>
Python内置函数(6)——round的更多相关文章
- Python内置函数(55)——round
英文文档: round(number[, ndigits]) Return the floating point value number rounded to ndigits digits afte ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- 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 ...
- python 内置函数总结(大部分)
python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...
- Python之路(第八篇)Python内置函数、zip()、max()、min()
一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...
- python内置函数大全(分类)
python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...
随机推荐
- error:Unterminated <form:form tag
问题:标签不对称 解决:<form:form></form> 改成 <form:form> </form:form> 虽然又是自动补全带来的bug,但还 ...
- 【python学习笔记】5.条件、循环和其他语句
[python学习笔记]5.条件.循环和其他语句 print: 用来打印表达式,不管是字符串还是其他类型,都输出以字符串输出:可以通过逗号分隔输出多个表达式 import: 导入模块 impo ...
- ajax提交表单、ajax实现文件上传
ajax提交表单.ajax实现文件上传,有需要的朋友可以参考下. 方式一:利用from表单的targer属性 + 隐藏的iframe 达到类似效果, 支持提交含有文件和普通数据的复杂表单 方式二:使用 ...
- 用JAVA进行Json数据解析(对象数组的相互嵌套)
这段时间我们在做一个英语翻译软件的小小小APP,涉及到了对Json数据的解析,所以特地来总结一下! 假设我们要对如下数据进行解析,其实在平时,返回的Json数据是很乱的,很难分清数据的关系,这是经过相 ...
- 【Linux】 文本比较工具 diff和cmp
Linux 文本比较工具 ■ diff命令 diff用于逐行比较两个文本文件,列出其不同之处 diff [option] <file1> <file2> file1和file2 ...
- 大数据 --> 淘宝异构数据源数据交换工具 DataX
淘宝异构数据源数据交换工具 DataX DataX是什么? DataX是一个在异构的数据库/文件系统之间高速交换数据的工具,实现了在任意的数据处理系统(RDBMS/Hdfs/Local filesys ...
- 目前微服务/REST的最佳技术栈
服务端 日期 编程语言 Web Framework DbAccess/ORM 数据库 2017-09-07 Node.js 8.4 Koa 2.3 sequelize 4.8 MySQL 2017-0 ...
- Sweet Butter 香甜的黄油
Sweet Butter 香甜的黄油 题目大意:m个点,n头奶牛,p条边,每一头奶牛在一个点上,一个点可以有多只奶牛,求这样一个点,使得所有奶牛到这个点的距离之和最小. 注释:n<=500 , ...
- Linux下ping,telnet,ssh命令的比较
ping工作在OSI模型的第三层,网络层. 主要用于测试到达目的主机的网络是否连接,不能检测某个端口是否开放. ping使用ICMP协议,不使用某个特定端口. 也可以 ping 域名 ,这样可以直接看 ...
- Python中的SQLAlchemy
在Python中,使用SQLAlchemy可以对数据库进行操作. SQLAlchemy是Python中的一个标准库. 要使用SQLAlchemy,首先要创建连接: url = mysql+pymysq ...