Python Cookbook - 数字的四舍五入 (
round(value, ndigits)
函数)
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
模块了。