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. 基于Kaggle的图像分类(CIFAR-10)
  2. LoadRunner之二“集合点”
  3. php 模块指令,php artisan module常用命令
  4. AI+云 华为开启智能时代新纪元
  5. redission收发命令流程分析
  6. 转:Linux--进程间通信(信号量,共享内存)
  7. 如何用c 控制mysql数据库_用C语言操作MySQL数据库
  8. Apache和PHP结合、Apache默认虚拟主机
  9. python processpoolexector 释放内存_python之ThreadPoolExecutor
  10. 完成端口(CompletionPort)详解 - 手把手教你玩转网络编程系列之三2-转
  11. htm5l,第一个script代码练习
  12. FPGA IP核分类
  13. 软件测试用例设计规范
  14. 2018tfe世界计算机专业排名,2018年TFE TIMES美国研究生计算机科学专业排名
  15. 云服务器安全配置开放哪些端口
  16. 求饶不经过原点的旋转轴的旋转矩阵
  17. 腾讯企业邮箱支持 pop/imap/exchange服务器地址(用于客户端)
  18. 使用Python在Excel表指定位置插入多列并赋值
  19. stc单片机自动下载程序原理和代码实现
  20. 王者荣耀qq区服务器位置,王者荣耀:qq区单排现状,射手可能是最难的一个位置,为什么?...

热门文章

  1. js实现颜色转换hex转rgba
  2. 【无标题】性价比超高的ARM Cortex-M0核32位单片机
  3. 桌面图标有阴影解决方案
  4. python excel转xml 用例_测试用例Excel转XML格式教程
  5. 均线系统之详解 —— 第一讲
  6. windows主题方面
  7. 这群白帽黑客,是网络世界的守夜人 ​
  8. 高效阅读英语信息的方法
  9. php 抽象对象,PHP5.0对象模型探索之抽象方法和抽象类
  10. java西历转换和历_java 西历转和历