python round函数

'''
round ( number [, ndigits ] )
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 number, round 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.
'''
'''
小数点后面第1位到第3位就是第0位、第1位、第2位
小数点前面的整数部分个位、十位、百位就是第-1位,第-2位,第-3位
如果当前指定的位不是5,则采用四舍六入,如果是5,就不好说了
'''
print(round(0.444))           # 0     类似于print(round(0.4,0)),但输出整数
print(round(0.444,0))         # 0.0   0表示小数点后面第1位,四舍
print(round(0.444,1))         # 0.4   1表示小数点后面第2位,四舍
print(round(0.444,2))         # 0.44  2表示小数点后面第3位,四舍
print(round(0.644,0))         # 1.0   0表示小数点后面第1位,六入
print(round(0.464,1))         # 0.5   1表示小数点后面第2位,六入
print(round(0.446,2))         # 0.45  2表示小数点后面第3位,六入
print(round(444.444,-1))      # 440.0  -1表示个位,四舍
print(round(444.444,-2))      # 400.0  -2表示十位,四舍
print(round(444.444,-3))      # 0.0  -3表示百位,四舍
print(round(446.444,-1))      # 450.0  -1表示个位,六入
print(round(464.444,-2))      # 500.0  -2表示十位,六入
print(round(644.444,-3))      # 1000.0  -3表示百位,六入

#===================================================================================================#
# 4.544 5.544 6.544 不在中点,可能是遵循四舍五入
print(round(444.544))           # 445     类似于round(444.544,0),但输出整数
print(round(444.544,0))         # 445.0   当前位是小数点后面第1位,但数值为5,前一位是4
print(round(445.544,0))         # 446.0  当前位是小数点后面第1位,但数值为5,前一位是5
print(round(446.544,0))         # 447.0  当前位是小数点后面第1位,但数值为5,前一位是6

print(round(444.500,0))         # 444.0   当前位是小数点后面第1位,但数值为5,前一位是4,4.5在4.0和5.0的中点,选择偶数4.0这一边
print(round(445.500,0))         # 446.0  当前位是小数点后面第1位,但数值为5,前一位是5,5.5在5.0和6.0的中点,选择偶数6.0这一边
print(round(446.500,0))         # 446.0  当前位是小数点后面第1位,但数值为5,前一位是6,6.5在6.0和6.5的中点,选择偶数6.0这一边

# 454 554 654 不在中点,可能是遵循四舍五入
print(round(444.454,1))         # 444.5   当前位是小数点后面第1位,但数值为5,前一位是4
print(round(445.554,1))         # 445.6  当前位是小数点后面第1位,但数值为5,前一位是5
print(round(446.654,1))         # 446.7  当前位是小数点后面第1位,但数值为5,前一位是6

print(round(444.450,1))         # 444.4   当前位是小数点后面第1位,但数值为5,前一位是4,450在400和500的中点,选择偶数4这一边
print(round(445.550,1))         # 445.6  当前位是小数点后面第1位,但数值为5,前一位是5,550在500和600之间的中点,选择偶数6这一边
print(round(446.650,1))         # 446.6  当前位是小数点后面第1位,但数值为5,前一位是6,650在600和700之间的中点,选择偶数6这一边

#===================================================================================================#
print(round(0.5,0))             # 0.0 当前位是小数点后面第1位,但数值为5,前一位是0,0.5在0和1之间,选择偶数0这一边
print(round(-0.5,0))            # -0.0
print(round(1.5,0))             # 2.0 当前位是小数点后面第1位,但数值为5,前一位是1,1.5在1和2之间,选择偶数2这一边

print(round(2.65,1))       # 2.67 当前位是小数点后面第3位,但数值为5,前一位是6,选择偶数6这一边
print(round(2.675,2))       # 2.67 当前位是小数点后面第3位,但数值为5,前一位是7,却没有选择偶数8,可能是因为精度问题。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值