您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在Python 3中,小数点后2位货币

在Python 3中,小数点后2位货币

使用金钱时,您通常希望尽可能晚地限制精度,以使乘法等操作不会累积舍入误差。在python 2和python 3中,您可以.quantize()使用Decimal任意精度:

unit_price = decimal.Decimal('8.0107')
quantity = decimal.Decimal('0.056')
price = unit_price * quantity
cents = decimal.Decimal('.01')
money = price.quantize(cents, decimal.ROUND_HALF_UP)
python 2022/1/1 18:30:54 有519人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶