由于 python3 包括python2.7 以后的round策略使用的是decimal.ROUND_HALF_EVEN
即Round to nearest with ties going to nearest even integer. 也就是只有在整数部分是奇数的时候, 小数部分才逢5进1; 偶数时逢5舍去。 这有利于更好地保证数据的精确性, 并在实验数据处理中广为使用。
ROUND_HALF_UP (to nearest with ties going away from zero), or
import decimal
from decimal import Decimal
print(decimal.getcontext())
context=decimal.getcontext() # 获取decimal现在的上下文
context.rounding = decimal.ROUND_HALF_UP #修改rounding策略
print(decimal.getcontext())
print(round(Decimal(1.5659898), 2))
print(round(Decimal(1.5344898), 2))
print(round(Decimal(2.5659898), 2))
print(round(Decimal(2.5359898), 2))
# with decimal.localcontext() as ctx:
# ctx.prec = 32
# ctx.rounding=decimal.ROUND_HALF_UP
# print(round(decimal.Decimal(2.669), 2))
# print(round(decimal.Decimal(1.635), 2))
运行结果:
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
1.57
1.53
2.57
2.54
Context(prec=28, rounding=ROUND_HALF_UP, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[Inexact, FloatOperation, Rounded], traps=[InvalidOperation, DivisionByZero, Overflow])
def round_up(number,power=0):
"""
实现精确四舍五入,包含正、负小数多种场景
:param number: 需要四舍五入的小数
:param power: 四舍五入位数,支持0-∞
:return: 返回四舍五入后的结果
"""
digit = 10 ** power
num2 = float(int(number * digit))
# 处理正数,power不为0的情况
if number>=0 and power !=0:
tag = number * digit - num2 + 1 / (digit * 10)
if tag>=0.5:
return (num2+1)/digit
else:
return num2/digit
# 处理正数,power为0取整的情况
elif number>=0 and power==0 :
tag = number * digit - int(number)
if tag >= 0.5:
return (num2 + 1) / digit
else:
return num2 / digit
# 处理负数,power为0取整的情况
elif power==0 and number<0:
tag = number * digit - int(number)
if tag <= -0.5:
return (num2 - 1) / digit
else:
return num2 / digit
# 处理负数,power不为0的情况
else:
tag = number * digit - num2 - 1 / (digit * 10)
if tag <= -0.5:
return (num2-1)/digit
else:
return num2/digit
print(round_up( 2.4259, 0))
print(round_up( -2.625, 2))
print(round_up( -2.635, 2))
print(round_up( 0, 0))
结果示例:
2.0
-2.63
-2.64
0.0
参考:
https://docs.python.org/2/library/decimal.html