java 复利贡献公式

java compound interest with contributions formula

我目前正在尝试开发一个包括每月供款的复利计算器。使用以下代码行,我已经成功地在没有每月供款的情况下进行复利计算,但无法弄清楚添加每月供款时的公式应该是什么。

double calculatedValue = (principalValue * Math.pow(1 + (interestRateValue/numberOfCompoundsValue), (termValue * numberOfCompoundsValue)));

在尝试通过贡献获取计算值时,我更改了完成此操作的方式。请参阅以下代码,我是如何处理这个问题的。

//The starting principal
double principalValue = 5000;

//Interest rate (%)
double interestRateValue = 0.05;

//How many times a year to add interest
int numberOfCompoundsValue = 4;

//The number of years used for the calculation
double termValue = 30;

//The monthly contribution amount
double monthlyContributionsValue = 400;

//How often interest is added. E.g. Every 3 months if adding interest 4 times in a year
int interestAddedEveryXMonths = 12/numberOfCompoundsValue;

//The total number of months for the calculation
int totalNumberOfMonths = (int)(12 * termValue);

    for(int i = 1; i <= totalNumberOfMonths; i++)
    {

        principalValue += monthlyContributionsValue;

        if(i % interestAddedEveryXMonths == 0)
        {
            principalValue += (principalValue * interestRateValue);
        }
    }

我认为这应该可以满足我的需求。每个月按供款额增加本金,如果该月等于应加利息的月份,则计算利息 * 利率并将其加到本金中。

当使用上面的值时,我期望答案是 355,242.18 美元,但得到的是 10511941.97 美元,这在我的银行账户中看起来更好,但在我的计算中却不是。

如果有人能给我一些帮助或指出我哪里做错了,将不胜感激。

提前致谢

经过一些简短的测试后,我得出的结论是:

  • 误算了您想要的价值 ($355,242.18)

  • 错误地问了你的问题

您所描述的计算方式($5000 开始 + 30 年每月供款 $400 + 每 3 个月利息)可通过您提供的代码找到。它给出的值($10,511,941.97)在我看来确实是正确的。我能提供的唯一其他建议是仅在需要时使用 double(例如 termValue 可以是 intAND您知道该值不会更改(例如 interestRateValue),请使用 final。这将有助于避免大型程序中出现任何不可预见的错误。我希望这可以帮助您计算出您的利息计算器或回答您的任何问题。

你的问题在这里:

principalValue += (principalValue * interestRateValue);

您每个季度都会增加一整年的利息,而您应该只增加四分之一的利息。您需要降低利率以获得正确的利率。

这是一个例子:

class CashFlow {
    private final double initialDeposit;
    private final double rate;
    private final int years;
    private final double monthlyContribution;
    private final int interestFrequency;

    CashFlow(double initialDeposit, double rate, int years,
             double monthlyContribution, int interestFrequency) {
        if ( years < 1 ) {
            throw new IllegalArgumentException("years must be at least 1");
        }

        if ( rate <= 0 ) {
            throw new IllegalArgumentException("rate must be positive");
        }

        if ( 12 % interestFrequency != 0 ) {
            throw new IllegalArgumentException("frequency must divide 12");
        }

        this.initialDeposit = initialDeposit;
        this.rate = rate;
        this.years = years;
        this.monthlyContribution = monthlyContribution;
        this.interestFrequency = interestFrequency;
    }

    public double terminalValue() {
        final int interestPeriod = 12 / interestFrequency;
        final double pRate = Math.pow(1 + rate, 1.0 / interestPeriod) - 1;
        double value = initialDeposit;

        for ( int i = 0; i < years * 12; ++i ) {
            value += monthlyContribution;

            if ( i % interestFrequency == interestFrequency - 1 ) {
                value *= 1 + pRate;
            }  
        }

        return value;
    }
}

class CompoundCalc {

    public static void main(String[] args) {
        CashFlow cf = new CashFlow(5000, 0.05, 30, 400, 3);
        System.out.println("Terminal value: " + cf.terminalValue());
    }
}

输出:

run:
Terminal value: 350421.2302849443
BUILD SUCCESSFUL (total time: 0 seconds)

接近您找到的 35.5 万美元的价值。

您可以使用多种不同的惯例来获取季度费率。将年率除以 4 是一种简单实用的方法,但上面的 pow(1 + rate, 1 / 4) - 1 方法在理论上更合理,因为它在数学上等同于相应的年率。

static void Main(string[] args)
        {
            double monthlyDeposit;
            double rateOfInterest;
            double numberOfCompounds;
            double years;
            double futureValue = 0;
            double totalAmount = 0;
Console.WriteLine("Compound Interest Calculation based on monthly deposits");
            Console.WriteLine("Monthly Deposit");
            monthlyDeposit = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Rate Of Interest");
            rateOfInterest = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Number of Compounds in a year");
            numberOfCompounds = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Number of year");
            years = Convert.ToDouble(Console.ReadLine());
            futureValue = monthlyDeposit;
            for (int i = 1; i <= years * 12; i++)
            {
                totalAmount = futureValue * (1 + (rateOfInterest / 100) / 12);
                if (i == years * 12)
                    futureValue = totalAmount;
                else
                    futureValue = totalAmount + monthlyDeposit;
            }
            Console.WriteLine("Future Value is=" + futureValue);
            Console.ReadLine();

        }

        //Output
        Compound Interest Calculation based on monthly Deposits
        Monthly Deposit
        1500
        Rate Of Interest
        7.5
        Number of Compounds in a year
        12
        Number of year
        1
        Future Value is=18748.2726237313