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 ...
随机推荐
- which framework or library is best to use WebRTC
which framework or library is best to use WebRTC http://stackoverflow.com/questions/24857637/current ...
- JavaScript编码规范(2)
变量 [强制] 变量.函数在使用前必须先定义. // good var name = 'MyName'; // bad name = 'MyName'; [强制] 每个 var 只能声明一个变量. 解 ...
- CPU平均负载 load average
平均负载是指上一分钟同时处于就绪状态的平均进程数.在CPU中可以理解为CPU可以并行处理的任务数量,就是CPU个数X核数.如果CPU Load等于CPU个数乘以核数,那么就说CPU正好满负载,再多一点 ...
- CMake基本语法
CMake简介 CMake 是做什么的? CMake是一套类似于automake的跨平台辅助项目编译的工具. 我觉得语法更加简单易用. CMake的工作流程 CMake处理顶级目录的CMakeList ...
- kubernetes实践之运行aspnetcore webapi微服务
1.预备工作 unbuntu 16.04 and above docker kubernetes 集群 2.使用vs2017创建一个web api应用程序,并打包镜像到本地. 3.推送本地镜像到doc ...
- smoking的安装和配置
yum install -y perl perl-Net-Telnet perl-Net-DNS perl-LDAP perl-libwww-perl perl-IO-Socket-SSL perl- ...
- 用bat文件启动mongodb
bat文件是dos下的批处理文件.批处理文件是无格式的文本文件,它包含一条或多条命令.它的文件扩展名为 .bat 或 .cmd.在命令提示下键入批处理文件的名称,或者双击该批处理文件,系统就会调用cm ...
- 【Zabbix】 Zabbix表结构说明【转载】
本文转自[https://www.cnblogs.com/shhnwangjian/p/5484352.html] 参考文[https://www.cnblogs.com/learningJAVA/p ...
- nginx配置防盗链
location ~* \.(gif|jpg|swf|flv|mp3|mp4|zip|rar)$ { root /home/soft; valid_referers *.qinyj.top downl ...
- Algorithm --> 字母重排
字母重排 输入一个字典(用***结尾),然后再输入若干单词.没输入一个单词w,都需要在字典中找出所有可以用w的字幕重排后得到的单词,并按照字典序从小到大的顺序在一行中输出,如果不存在,输出“:(”.单 ...