Python Round()函数舍入问题

前言

过去在Python中使用到Round()函数时,都是默认四舍五入,直到刚才一小段代码运行出错,最后追溯到了Round()函数,网上冲浪外加官方文档才发现它水很深;鉴于之前经常出现遇到问题,网上搜索后解决完了,后续再遇到又一脸懵的情况,决定今后遇到的问题尽量记录下来,作为笔记使用

一、Pytho中的 round()函数

作为Python内置函数,可以根据给定的精度对一个数字进行舍入操作

二、文档

1.

目录

前言

一、Pytho中的 round()函数

二、文档

1.help(round)

2.官方文档

round(x, n)

n<0

n>0

总结


help(round)

解释器自带帮助文档结果如下

Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.
    
    The return value is an integer if ndigits is omitted or None.  Otherwise
    the return value has the same type as the number.  ndigits may be negative.

2.官方文档

链接:round (Python function, in Built-in Functions)

round(numberndigits=None)

Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.

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, both round(0.5) and round(-0.5) are 0,and round(1.5) is 2). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if ndigits is omitted or None. Otherwise, the return value has the same type as number.

For a general Python object numberround delegates to number.__round__.

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.

文档中提到,对于

round(x, n)

会将x以a\ast 10^{-n}(a为整数)的形式舍入到最接近x的情况;

例如round(3.141,1)中,要求精度10^{-1}= 0.1,现在需要决定的舍(a=31)还是入(a=32)

\left | 31*0.01-3.14 \right |=0.04<0.06=\left | 32*0.01-3.14 \right |

按照最接近的原则,最后决定直接舍去要求精度以外数位的值,保留1位小数3.1即可,这与小学学过的四舍五入无异,但是在一样接近时(精度后只有一位且为5)时,应当使结果向偶数舍入

l=[1.5,2.5,3.5,4.5]
for i in l:
    b = round(i)
    print(f'{i}  {b}')

运行结果符合文档所述

1.5  2
2.5  2
3.5  4
4.5  4

但关于偶数有一个小问题,只有整数才存在奇偶的问题,即文档中只说明了n未声明/为None/=0时的结果,针对其它情况,测试一番

  • n<0

l=[1.5,2.5,3.5,4.5]
for i in l:
    b = round(10*i,-1)
    print(f'{i}  {b}')
1.5  20.0
2.5  20.0
3.5  40.0
4.5  40.0

从运行结果可看出,n<0时仍然符合上述说明,但此时的偶数指的是a\ast 10^{-n}中a的奇偶,这点需要注意

  • n>0

l=[1.5,2.5,3.5,4.5]
for i in l:
    a = round(i/10+0.001,1)
    b = round(i/10,1)
    print(f'{i/10}  {b}  |  {i/10+0.001}  {a}')
0.15  0.1  |  0.151  0.2
0.25  0.2  |  0.251  0.3
0.35  0.3  |  0.351  0.4
0.45  0.5  |  0.451  0.5

运行结果表明n>0时偶数规则不再适用,此时遵循“四舍六入、50舍51入”的规则


 

总结

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值