首页 > 解决方案 > 最小和最大功能来计算最小信用卡余额

问题描述

我正在尝试使用最小和最大函数编写 Python 代码来计算信用卡上的最小余额。它是这样计算的:。最低付款额等于客户余额的 10 美元或 2.1%,以较高者为准;但如果这超过了余额,那么最低付款就是余额。这是我的代码:

如果余额是 600 balance = 600 customerInterest = max((balance*0.021),10.00) print(customerInterest) 如果余额是 11 balance = 11 customerInterest = min(max((balance * 0.021, 10.00)), balance) print (客户兴趣)

标签: pythonfunctionmaxmin

解决方案


使用 Python 3(示例客户余额为 543.21 美元):

from decimal import *

def calculateMinPayment(customerBalance: Decimal) -> Decimal:
    if not isinstance(customerBalance, Decimal):
        raise TypeError('unsupported parameter type for customerBalance')
    return min(max(Decimal('10'), customerBalance * Decimal('0.021')), customerBalance).quantize(Decimal('.01'), rounding=ROUND_HALF_UP)

print(calculateMinPayment(Decimal('543.21')))

推荐阅读