🛠️ToolsShed

复利计算器

计算投资随时间增长的复利。

复利计算器显示当利息不仅在本金上积累,还在以前期间的累积利息上积累时,投资或储蓄账户如何随时间增长。据说阿尔伯特·爱因斯坦称复利为世界第八大奇迹——利率或时间的微小差异在长期内可能会产生截然不同的结果。

输入您的本金金额、年利率、复利频率(每日、每月、每季度或每年)和年数。计算器显示最终余额、总利息收入以及逐年明细,以便您准确了解增长如何随时间加速。

理解复利对于退休规划、评估储蓄账户、比较贷款成本和做出明智的投资决策至关重要。关键洞察是,早开始并再投资回报可以产生比稍后以更高利率开始显著更多的财富。

常见问题

代码实现

def compound_interest(principal, annual_rate, years, n=12, contribution=0):
    """
    Calculate compound interest with optional regular contributions.

    Parameters:
        principal    - initial investment
        annual_rate  - annual interest rate (e.g. 0.07 for 7%)
        years        - investment duration in years
        n            - compounding frequency per year (12 = monthly)
        contribution - periodic contribution (same frequency as n)

    Returns dict with final balance, total invested, and total interest.
    """
    r = annual_rate / n
    periods = int(years * n)
    balance = principal
    for _ in range(periods):
        balance = balance * (1 + r) + contribution
    total_invested = principal + contribution * periods
    return {
        "final_balance":  balance,
        "total_invested": total_invested,
        "total_interest": balance - total_invested,
    }

def rule_of_72(annual_rate):
    """Estimate years to double at a given annual rate."""
    return 72 / (annual_rate * 100)

# Simple formula (no contributions)
def compound_formula(P, r, n, t):
    """A = P(1 + r/n)^(nt)"""
    return P * (1 + r / n) ** (n * t)

# Examples
result = compound_interest(10000, 0.07, 20, n=12, contribution=500)
print(f"Final balance:   ${result['final_balance']:,.2f}")
print(f"Total invested:  ${result['total_invested']:,.2f}")
print(f"Total interest:  ${result['total_interest']:,.2f}")
print(f"Years to double: {rule_of_72(0.07):.1f} years at 7%")

# Compounding frequency comparison
for label, n in [("Annual", 1), ("Monthly", 12), ("Daily", 365)]:
    A = compound_formula(10000, 0.12, n, 10)
    print(f"{label:8}: ${A:,.2f}  (EAR = {((1 + 0.12/n)**n - 1)*100:.4f}%)")

Comments & Feedback

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