如何将经常性利率添加到经常性和不断增长的金额

时间:2016-09-22 17:43:34

标签: javascript

我试图在几个月内反复将这个给定的百分比添加到一个已经有上个月增加百分比的数字上。

即,用户定义了25%(未设置可能是任意数量的百分比)然后我将他们投入的金额增加25%,例如:

客户投资10,000英镑,我将25%增加到10,000英镑,相当于12,500英镑。 AND THEN 接下来的一个月,我将25%加上前一个月的12,500英镑,应该等于15,625英镑。

应该是简单数学的情况,但我无法用Javascript来解决这个问题。我不断获得15,000英镑的价值,并且无法弄清楚如何将给定百分比存储在变量本身中,然后将该百分比添加到总金额中。

这是一些代码。

// Set the values...
	 Num = prompt("Enter a percentage using a decimal Number...");
	 interestRate = Num*100;
	 startCash = 10000;
	 total = startCash*interestRate/100+startCash;
	 month = 1;
	 
	 // Inputting Text...
	 StartText = "Starting Money: £";
	 IntText = "Interest Earned: ";
	 TotalText = "Total Amount: £";
	 MonthText = "Month: ";
	 
	 displayStart = StartText + startCash + "\n";
	 dispInt = IntText + interestRate + "\n";
	 dispTotal = TotalText + total + "\n";
	 dispMonth = MonthText + month + "\n";


	 dispvalue = displayStart + dispInt + dispTotal + dispMonth;
	 


	 console.log (dispvalue);

	 addInt = total + interestRate*100;

	 console.log (addInt);

3 个答案:

答案 0 :(得分:1)

下个月计算的复利的总数应为

addInt = total*interestRate/100 + total;

就像代码的第五行一样。

答案 1 :(得分:1)

您可以执行以下操作;

var interest = 0.25,   // monthly interest
      curVal = 10000,  // current value
    duration = 12,
         sum = Array(duration).fill(interest)
                              .map((c,i) => curVal*Math.pow(1+c,i));
console.log(sum);

每个月的开始,我们都在计算复利,并将其应用于10,000的面值。

答案 2 :(得分:1)

您也可以尝试这样的方法:

var interestRate = .25;

var startCash = 10000;

var total=startCash;

for(var monthCount=1; monthCount<13;monthCount++){

    //this line takes the previous value of total and gets the percentage of interest
    //it is then re-assigned back to the same variable
    total += total*interestRate; 

    console.log('month :', monthCount);
    console.log('total :', total);
 }