🛠️ToolsShed

信用卡还款计算器

计算还清信用卡债务所需的时间和总利息。

最低还款:max(余额的2%, $25)

目标还款期

常见问题

代码实现

def payoff_simulation(balance, annual_rate, monthly_payment):
    """
    Simulate credit card payoff month by month.

    Parameters:
        balance         - current balance owed
        annual_rate     - annual interest rate (e.g. 0.20 for 20%)
        monthly_payment - fixed payment made each month

    Returns dict with months, total_paid, and total_interest.
    """
    monthly_rate  = annual_rate / 12
    months        = 0
    total_paid    = 0.0
    total_interest = 0.0

    while balance > 0:
        interest   = balance * monthly_rate
        total_interest += interest
        balance   += interest
        payment    = min(monthly_payment, balance)
        balance   -= payment
        total_paid += payment
        months    += 1
        if months > 1200:          # safety cap (100 years)
            break

    return {
        "months":         months,
        "total_paid":     total_paid,
        "total_interest": total_interest,
    }


def required_payment(balance, annual_rate, months):
    """
    Calculate fixed monthly payment to clear a balance in exactly N months.
    Standard amortization formula: M = P * r / (1 - (1+r)^-n)
    """
    r = annual_rate / 12
    if r == 0:
        return balance / months
    return balance * r / (1 - (1 + r) ** -months)


def min_payment(balance, min_percent=0.02, min_floor=25.0):
    """Return the minimum payment (2% of balance, at least $25)."""
    return max(balance * min_percent, min_floor)


# Example: $5,000 balance at 20% APR, paying $150/month
result = payoff_simulation(5000, 0.20, 150)
print(f"Months to pay off:  {result['months']}")
print(f"Total paid:         ${result['total_paid']:,.2f}")
print(f"Total interest:     ${result['total_interest']:,.2f}")

# Required payment to pay off in 24 months
payment = required_payment(5000, 0.20, 24)
print(f"\nRequired for 24 months: ${payment:,.2f}/mo")

# Minimum payment scenario
mp = min_payment(5000)
mp_result = payoff_simulation(5000, 0.20, mp)
print(f"Minimum payment (${mp:.2f}/mo) takes {mp_result['months']} months")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.