美文网首页
python使用round函数的bug

python使用round函数的bug

作者: frankie_cheung | 来源:发表于2019-04-26 16:33 被阅读0次

在对小数1.275进行四舍五入时,出现了bug,


a=1.275

print(round(a,2))
结果为:1.27

这肯定是不对的呀,百度一下 有一个这个可以用:

from _pydecimal import Decimal,Context,ROUND_HALF_UP
def my_round(str_num):
    return (Context(prec=3,rounding=ROUND_HALF_UP).create_decimal('{}'.format(str_num)))

UPDATE:
上述方法还是不行,使用这个:

from decimal import Decimal
new_index=Decimal('{}'.format(old_index)).quantize(Decimal('0.00'))

相关文章

网友评论

      本文标题:python使用round函数的bug

      本文链接:https://www.haomeiwen.com/subject/ksdqnqtx.html