gpt4 book ai didi

java复利与贡献公式

转载 作者:行者123 更新时间:2023-12-01 11:40:08 24 4
gpt4 key购买 nike

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

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 美元,这在我的银行帐户中看起来更好,但在我的计算中却不是。

如果有人可以向我提供一些帮助或指出我出错的地方,我将不胜感激。

提前致谢

最佳答案

您的问题在这里:

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)

这接近您找到的 355,000 美元的值(value)。

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

关于java复利与贡献公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29597068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com