【python基础笔记-3】decimal模块解决浮点数计算精度问题

时间:2021-09-06
本文章向大家介绍【python基础笔记-3】decimal模块解决浮点数计算精度问题,主要包括【python基础笔记-3】decimal模块解决浮点数计算精度问题使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

通过Decimal('123.456')对象实例化后做 + - * / 等运算符操作计算结果不会出现精度问题。

Tips:值得注意的2点是

1.Decimal接收的入参是str,所以如果原本操作的数据类型是float需要提前强转为str。

2.decimal模块中getcontext().prec属性可以设置小数位,但是该设置是全局的,有可能会影响计算结果,比如a/b计算之前a和b已经被四舍五入2位小数了,再通过计算获得的结果必然不准,所以不如必要使用该值时劲量设大点,或者不用该设置,直接把计算结果round处理。

from decimal import Decimal, getcontext
from datetime import datetime
from dateutil.relativedelta import relativedelta


class RepaymentCalculator():
    # 等额本金还款计划试算
    def EP_cal(self, amount, apr, installments: int, start_date=None) -> list:
        # getcontext().prec = 2   ,此操作有风险
        per_principal = Decimal(str(amount)) / Decimal(str(installments))
        mpr = Decimal(str(apr)) / Decimal(str('360')) * Decimal(str('30'))
        # print(float(mpr))   ,如果上面设置了prec=2此时计算出来的mpr必然不准确
        if start_date == None:
            start_date = datetime.today()
        else:
            start_date = datetime.strptime(start_date, '%Y-%m-%d')
        plans = []
        for i in range(0, installments):
            installment_interest_start_date = start_date + relativedelta(months=i)
            plan_info = {}
            principal = Decimal(str(amount)) - Decimal(str(per_principal)) * Decimal(str(i))
            interest = Decimal(str(principal)) * Decimal(str(mpr))
            plan_info['principal'] = round(float(per_principal), 2)  # 先通过Decimal计算,再float转换,再round取精度
            plan_info['interest'] = round(float(interest), 2)
            plan_info['installment'] = i + 1
            plan_info['installment_interest_start_date'] = datetime.strftime(installment_interest_start_date,
                                                                             '%Y-%m-%d')
            plan_info['due_date'] = datetime.strftime(installment_interest_start_date + relativedelta(months=1),
                                                      '%Y-%m-%d')
            plans.append(plan_info)
        return plans

原文地址:https://www.cnblogs.com/wayne-tou/p/15235003.html