Python Cookbook - 数字的四舍五入 (round(value, ndigits) 函数)

Python Cookbook 3rd Edition - Documentation
https://python3-cookbook.readthedocs.io/zh_CN/latest/index.html

Python Cookbook 3rd Edition - GitHub
https://github.com/yidao620c/python3-cookbook

1. 数字日期和时间

1.1 数字的四舍五入

浮点数执行指定精度的舍入运算。

对于简单的舍入运算,使用内置的 round(value, ndigits) 函数即可,特别注意 round 函数返回值为浮点数。

strong@foreverstrong:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> round(2.22, 1)
2.2
>>>
>>> round(2.38, 1)
2.4
>>>
>>> round(-1.26, 1)
-1.3
>>>
>>> round(1.23456, 4)
1.2346
>>> exit()
strong@foreverstrong:~$

当一个值刚好在两个边界的中间的时候, round 函数返回离它最近的偶数。对 1.5 或者 2.5 的舍入运算都会得到 2。

四舍六入五成双。这里“四”是小于五的意思,“六”是大于五的意思,“五”是舍入位之后的尾数逢五的话看前一位,奇进偶不进。

(base) yongqiang@yongqiang:~$ python3
Python 3.9.5 (default, Jun  4 2021, 12:28:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.5)
0
>>>
>>> round(1.5)
2
>>>
>>> round(2.5)
2
>>>
>>> round(3.5)
4
>>>
>>> round(4.5)
4
>>>
>>> round(5.5)
6
>>>
>>> round(6.5)
6
>>>
>>> round(7.5)
8
>>>
>>> round(8.5)
8
>>>
>>> round(9.5)
10
>>>
>>> exit()
(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ python3
Python 3.9.5 (default, Jun  4 2021, 12:28:51)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> round(1.5)
2
>>>
>>> round(2.5)
2
>>>
>>> round(3.4, 0)
3.0
>>>
>>> round(3.5, 0)
4.0
>>>
>>> round(3.6, 0)
4.0
>>>
>>> round(3.05, 1)
3.0
>>>
>>> round(3.15, 1)
3.1
>>>
>>> round(3.25, 1)
3.2
>>>
>>> round(3.35, 1)
3.4
>>>
>>> round(3.45, 1)
3.5
>>>
>>> round(3.55, 1)
3.5
>>>
>>> round(3.65, 1)
3.6
>>>
>>> round(3.75, 1)
3.8
>>>
>>> round(3.85, 1)
3.9
>>>
>>> round(3.95, 1)
4.0
>>>
>>> round(4.05, 1)
4.0
>>>
>>> round(4.15, 1)
4.2
>>>
>>> round(4.25, 1)
4.2
>>>
>>> round(4.35, 1)
4.3
>>>
>>> round(4.45, 1)
4.5
>>>
>>> round(4.55, 1)
4.5
>>>
>>> round(4.65, 1)
4.7
>>>
>>> round(4.75, 1)
4.8
>>>
>>> round(4.85, 1)
4.8
>>>
>>> round(4.95, 1)
5.0
>>>
>>> exit()
(base) yongqiang@yongqiang:~$

传给 round() 函数的 ndigits 参数可以是负数,这种情况下,舍入运算会作用在十位、百位、千位等上面。

strong@foreverstrong:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> num = 32768
>>>
>>> round(32768, 1)
32768
>>>
>>> round(32768, 0)
32768
>>>
>>> round(32768, -1)
32770
>>>
>>> round(32768, -2)
32800
>>>
>>> round(32768, -3)
33000
>>>
>>> round(32768, -4)
30000
>>>
>>> num = 12345
>>>
>>> round(num, -1)
12340
>>>
>>> round(num, -2)
12300
>>>
>>> num = 15253545
>>>
>>>
>>> round(num, -1)
15253540
>>>
>>> round(num, -3)
15254000
>>>
>>> round(num, -5)
15300000
>>>
>>> round(num, -7)
20000000
>>>
>>> exit()
strong@foreverstrong:~$

不要将舍入和格式化输出混淆了。如果你的目的只是简单的输出一定宽度的数,不需要使用 round() 函数。 而仅仅只需要在格式化的时候指定精度即可。

strong@foreverstrong:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> num = 1.2345678
>>>
>>> format(num, '0.2f')
'1.23'
>>>
>>> format(num, '0.3f')
'1.235'
>>>
>>> format(num, '0.4f')
'1.2346'
>>>
>>> "value is {:0.3f}".format(num)
'value is 1.235'
>>>
>>> "value is {:0.4f}".format(num)
'value is 1.2346'
>>>
>>> exit()
strong@foreverstrong:~$

不要试着去舍入浮点值来修正表面上看起来正确的问题。

strong@foreverstrong:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> num1 = 1.2
>>>
>>> num2 = 2.2
>>>
>>> num12 = num1 + num2
>>> num12
3.4000000000000004
>>>
>>> num12 = round(num12, 2)
>>> num12
3.4
>>> exit()
strong@foreverstrong:~$

尽管在计算的时候会有一点点小的误差,但是这些小的误差是能被理解与容忍的。 如果不能允许这样的误差 (金融领域),那么就得考虑使用 decimal 模块了。

References

https://yongqiang.blog.csdn.net/

Python Cookbook - 数字的四舍五入 (round(value, ndigits) 函数)相关推荐

  1. python 四舍五入 round( x [, n] )函数 int()函数

    round()函数 > round( x [, n] ) 参数x,n均为数值表达式,返回值为x的四舍五入值.n为保留的小数位数,不加n则只保留x四舍五入后的整数部分. round()函数只有一个 ...

  2. 《Python Cookbook 3rd》笔记(3.1):数字的四舍五入

    数字的四舍五入 问题 你想对浮点数执行指定精度的舍入运算. 解法 对于简单的舍入运算,使用内置的 round(value, ndigits) 函数即可.比如: >>> round(1 ...

  3. 使用python函数计算3.5四舍五入的结果_python 数字的四舍五入-Go语言中文社区

    python 数字的四舍五入 问题 你想对浮点数执行指定精度的舍入运算. 解决方案 对于简单的舍入运算,使用内置的 round(value, ndigits) 函数即可.比如: 当一个值刚好在两个边界 ...

  4. python中trunc函数_Oracle trunc()函数的用法及四舍五入 round函数

    --Oracle trunc()函数的用法 /**************日期********************/ 1.select trunc(sysdate) from dual  --20 ...

  5. Notes of Python Cookbook (Chr1-Chr3)

    贴子主要关于自己的读书摘要,涉及到侵权请联系删除 第一章:数据结构和算法 # 任何的序列(或者是可迭代对象)可以通过一个简单的赋值语句解压并赋值给多 # 个变量.唯一的前提就是变量的数量必须跟序列元素 ...

  6. 《Python Cookbook 3rd》笔记汇总

    文章目录 一.数据结构 二.字符串和文本 三.数字.日期和时间 四.迭代器与生成器 五.文件与IO 一.数据结构 标题 关键词 1.1:拆分序列后赋值给多个变量 可迭代对象.拆分赋值 1.2:拆分任意 ...

  7. Python Cookbook手记I

    Chap1 数据结构与算法 从任意长度的可迭代对象中分解元素 "*表达式"可以用来将一个含有N个元素的数据结构类型分解成所需的几部分. 例如grades保存了100个成绩数据而我们 ...

  8. 递归函数合式分解python_学习python的day10之递归与内置函数

    一.递归 递归的特点: 函数内部自己调用自己 必须出口 需求:求3以内的累加和 defsum(a):if a == 1:return 1 return a+sum(a-1) result= sum(3 ...

  9. floor()函数与round()函数

    floor函数 floor函数取整,保留整数部分,舍弃小数部分,当时负数部分时,向远离0的方向取值 例如: math.floor(1.5) = 1.0 math.floor(-1.5) = -2.0 ...

最新文章

  1. 802.11协议之BA/BAR帧
  2. 正则表达式中的小括号用法
  3. android 文字路径,Android自定义控件:路径及文字
  4. 阿里云李飞飞:传统数据库步履蹒跚,未来的机会在哪里?
  5. Linux 下gedit编辑器的使用
  6. Knowledge Graph Alignment Network with Gated Multi-Hop Neighborhood Aggregation-学习笔记
  7. openssl windows安装
  8. android.mk 冒号,android学习-ndk-build(androidstudio编译cocos2d-x库的cpp为so文件的解释)
  9. Cocos2d-x简介
  10. jquery使用规则
  11. oracle 空间详解,Oracle Spatial空间分析详解 | 学步园
  12. 2015061403 - firebug下载地址
  13. Windows 7 插件KB4474419安装 Symantec Endpoint Protection 只能安装在具有SHA-2代码签名支持更新(KB4474419)的系统上
  14. 汕头大学计算机转专业,2021年汕头大学大一新生转专业及入学考试相关规定
  15. 微信收货地址开发分享
  16. 【图像去噪】基于matlab小波滤波(硬阙值+软阙值)+中值滤波图像去噪【含Matlab源码 462期】
  17. xlsx无法导入MySQL?
  18. Linux下dirname命令
  19. STC全系列头文件及用户手册(官方资源的获取方法)
  20. 计算机视觉工程师装机软件一览

热门文章

  1. 实用文库类网站推荐,必须收藏,工作学习都需要
  2. 模型预测控制的缺点_华北电力大学 刘英培等:适用于风电并网的VSCHVDC系统模型预测控制...
  3. 百度地图 卫星 二维
  4. 一篇文章搞懂 HDFS 的 Archive 到底是什么
  5. linux系统修改普通用户密码和破解管理员密码
  6. MOTOROLA MC40 android系统扫描开发
  7. unity的HDR效果
  8. 虚拟机使用vm8模式上网
  9. 利用Win32 API写一个支持多坐标的鼠标连点器.
  10. python 自动聊天机器人_IT之家学院:让你的微信号变成自动聊天机器人