python round()函数舍入说明及规则

round() 方法返回浮点数x的四舍五入值。

以下是 round() 方法的语法:

round( x [, n]  )
  • x -- 数值表达式,需要四舍五入的数。
  • n -- 数值表达式,需要小数点后保留的位数。

返回浮点数x的四舍五入值:

指定的位数大于 0,返回四舍五入到指定的小数位;

指定的位数等于 0,返回四舍五入到最接近的整数,保留整数部分;

指定的位数小于 0,对整数部分进行四舍五入,返回的结果是浮点数。

四舍五入规则:

对于位数很多的近似数,当有效位数确定后,其后面多余的数字应该舍去,只保留有效数字最末一位,这种修约(舍入)规则是“四舍六入五成双”,也即“4舍6入5凑偶”,这里“四”是指≤4 时舍去,"六"是指≥6时进上,"五"指的是根据5后面的数字来定,当5后有数时,舍5入1;当5后无有效数字时,需要分两种情况来讲:
(1)5前为奇数,舍5入1;
(2)5前为偶数,舍5不进(0是偶数)。

例如:

print(round(1.35, 1))  # 1.4
print(round(2.35, 1))  # 2.4
print(round(1.135, 2))  # 1.14
print(round(1.235, 2))  # 1.24
print(round(1.335, 2))  # 1.33
print(round(1.435, 2))  # 1.44
print(round(1.535, 2))  # 1.53
print(round(1.635, 2))  # 1.64
print(round(1.735, 2))  # 1.74
print(round(1.835, 2))  # 1.83
print(round(1.935, 2))  # 1.99
print()

不要奇怪上边的代码运行结果。规则没有错:

a = [1.35, 2.35, 1.135, 1.235, 1.335, 1.435, 1.535, 1.635, 1.735, 1.835, 1.935,
     1.75, 2.75, 1.175, 1.275, 1.375, 1.475, 1.575, 1.675, 1.775, 1.875, 1.975,
     1.85, 2.85, 1.185, 1.285, 1.385, 1.485, 1.585, 1.685, 1.785, 1.885, 1.985]

for va in a:
    print("{}".format(va))
    print("Decimal({}): {}".format(va, Decimal(va)))
    print('round({}, {}): {}'.format(va, len(str(va)) - 3, round(va, len(str(va)) - 3)))
    print() 

运行结果如下:

1.35
Decimal(1.35): 1.350000000000000088817841970012523233890533447265625
round(1.35, 1): 1.4

2.35
Decimal(2.35): 2.350000000000000088817841970012523233890533447265625
round(2.35, 1): 2.4

1.135
Decimal(1.135): 1.1350000000000000088817841970012523233890533447265625
round(1.135, 2): 1.14

1.235
Decimal(1.235): 1.2350000000000000976996261670137755572795867919921875
round(1.235, 2): 1.24

1.335
Decimal(1.335): 1.33499999999999996447286321199499070644378662109375
round(1.335, 2): 1.33

1.435
Decimal(1.435): 1.435000000000000053290705182007513940334320068359375
round(1.435, 2): 1.44

1.535
Decimal(1.535): 1.5349999999999999200639422269887290894985198974609375
round(1.535, 2): 1.53

1.635
Decimal(1.635): 1.6350000000000000088817841970012523233890533447265625
round(1.635, 2): 1.64

1.735
Decimal(1.735): 1.7350000000000000976996261670137755572795867919921875
round(1.735, 2): 1.74

1.835
Decimal(1.835): 1.83499999999999996447286321199499070644378662109375
round(1.835, 2): 1.83

1.935
Decimal(1.935): 1.935000000000000053290705182007513940334320068359375
round(1.935, 2): 1.94

1.75
Decimal(1.75): 1.75
round(1.75, 1): 1.8

2.75
Decimal(2.75): 2.75
round(2.75, 1): 2.8

1.175
Decimal(1.175): 1.1750000000000000444089209850062616169452667236328125
round(1.175, 2): 1.18

1.275
Decimal(1.275): 1.274999999999999911182158029987476766109466552734375
round(1.275, 2): 1.27

1.375
Decimal(1.375): 1.375
round(1.375, 2): 1.38

1.475
Decimal(1.475): 1.475000000000000088817841970012523233890533447265625
round(1.475, 2): 1.48

1.575
Decimal(1.575): 1.5749999999999999555910790149937383830547332763671875
round(1.575, 2): 1.57

1.675
Decimal(1.675): 1.6750000000000000444089209850062616169452667236328125
round(1.675, 2): 1.68

1.775
Decimal(1.775): 1.774999999999999911182158029987476766109466552734375
round(1.775, 2): 1.77

1.875
Decimal(1.875): 1.875
round(1.875, 2): 1.88

1.975
Decimal(1.975): 1.975000000000000088817841970012523233890533447265625
round(1.975, 2): 1.98

1.85
Decimal(1.85): 1.850000000000000088817841970012523233890533447265625
round(1.85, 1): 1.9

2.85
Decimal(2.85): 2.850000000000000088817841970012523233890533447265625
round(2.85, 1): 2.9

1.185
Decimal(1.185): 1.185000000000000053290705182007513940334320068359375
round(1.185, 2): 1.19

1.285
Decimal(1.285): 1.2849999999999999200639422269887290894985198974609375
round(1.285, 2): 1.28

1.385
Decimal(1.385): 1.3850000000000000088817841970012523233890533447265625
round(1.385, 2): 1.39

1.485
Decimal(1.485): 1.4850000000000000976996261670137755572795867919921875
round(1.485, 2): 1.49

1.585
Decimal(1.585): 1.58499999999999996447286321199499070644378662109375
round(1.585, 2): 1.58

1.685
Decimal(1.685): 1.685000000000000053290705182007513940334320068359375
round(1.685, 2): 1.69

1.785
Decimal(1.785): 1.7849999999999999200639422269887290894985198974609375
round(1.785, 2): 1.78

1.885
Decimal(1.885): 1.8850000000000000088817841970012523233890533447265625
round(1.885, 2): 1.89

1.985
Decimal(1.985): 1.9850000000000000976996261670137755572795867919921875
round(1.985, 2): 1.99


Process finished with exit code 0

所以round()函数规则是“四舍六入五成双”,只是要注意是数字的实际大小,而不是你看到的大小。

至于Decimal(1.885)=1.8850000000000000088817841970012523233890533447265625,这是浮点数精度丢失问题,可以参考:

浮点数的存储方式_yolo_yyh的博客-CSDN博客

【Java基础】浮点数精度丢失问题剖析_Money、坤的博客-CSDN博客

使用java运行

        double d = 20014999;
        long l = Double.doubleToLongBits(d);
        System.out.println("Double  :"+Long.toBinaryString(l));
        float f = 20014999;
        int i = Float.floatToIntBits(f);
        System.out.println("Float   :"+Integer.toBinaryString(i));

得到:

Double  :2.0014999E7
Double  :100000101110011000101100111100101110000000000000000000000000000
Float   :2.0015E7
Float   :1001011100110001011001111001100

即:

	符号位    	指数位            	尾数位
Float	0	       10010111 	00110001011001111001100
Double	0	    10000010111 	0011000101100111100101110000000000000000000000000000

可以看到float的尾数无法正确表示 20014999f,这就是浮点数精度丢失的原因。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值