使用 JavaScript 计算复利的未来价值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4712585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating future value with compound interest with JavaScript
提问by Alex Berg
I'm trying to work on a script where the user inserts a monthly income and gets the future value with compound interest after 30 years. As it is now, I've assigned some values for testing purposes.
我正在尝试编写一个脚本,其中用户插入月收入并在 30 年后以复利获得未来价值。就像现在一样,我已经分配了一些用于测试目的的值。
// Future Value
var investment = 800;
var annualRate = 2;
var monthlyRate = annualRate / 12 / 100;
var years = 30;
var months = years * 12;
var futureValue = 0;
for ( i = 1; i <= months; i++ ) {
futureValue = futureValue + investment * Math.pow(1 + monthlyRate, months);
}
Problem is, I'm actually building this from an Excel spreadsheet that's using the built-in FV() formula and when cross checking, my results are totally off... Any idea what I'm doing wrong since I'm not into finance math at all. Thanks in advance.
问题是,我实际上是从使用内置 FV() 公式的 Excel 电子表格构建的,当交叉检查时,我的结果完全不正确......知道我做错了什么,因为我不喜欢金融数学。提前致谢。
回答by David Tang
The Math.powis unnecessary, since you are calculating and incrementing futureValuemonth by month. Simply multiply by 1 + monthlyRate. You also want to add the current value of the investment to the new investment before multiplying:
这Math.pow是不必要的,因为您正在futureValue逐月计算和递增。简单地乘以1 + monthlyRate。您还想在乘法之前将投资的当前值添加到新投资中:
for ( i = 1; i <= months; i++ ) {
futureValue = (futureValue + investment) * (1 + monthlyRate);
}
Alternatively, you can also calculate this in one go with the following formula:
或者,您也可以使用以下公式一次性计算:
futureValue = investment * (Math.pow(1 + monthlyRate, months) - 1) / monthlyRate;
回答by Yasin Sunni
Below is the code to calculate compound interest.
下面是计算复利的代码。
function calculate() {
p = document.getElementById("p").value;
n = document.getElementById("n").value; // no. of compoundings per year
t = document.getElementById("t").value; // no. of years
r = document.getElementById("r").value;
result = document.getElementById("result");
// The equation is A = p * [[1 + (r/n)] ^ nt]
A = (p * Math.pow((1 + (r / (n * 100))), (n * t)));
// toFixed is used for rounding the amount with two decimal places.
result.innerHTML = "The total amount is " + A.toFixed(2);
result.innerHTML += "<br> The interest is " + (A.toFixed(2) - p).toFixed(2);
}
div {
display: table-row;
}
label,
input {
display: table-cell;
}
<html>
<head>
<title>Compound Interest Calculation using jQuery</title>
</head>
<body>
<h1>Compound Interest Calculation Using jQuery</h1>
<div> <label>Amount: </label> <input id="p"> </div>
<div> <label>Rate (%): </label> <input id="r"> </div>
<div> <label>No. of Years: </label> <input id="t"> </div>
<div> <label>Compunding Times Per Year: </label> <input id="n" value="1"> </div>
<button onclick="calculate()">Calculate</button>
<p id="result"></p>
</body>
</html>
回答by user3349578
function FVcalc(PresentAmount,InterestRate,NumberOfYears) {
var timescompound = 1;
var AnnualInterestRate = (InterestRate/100)/timescompound;
var Years= NumberOfYears
var Periods=timescompound*Years;
var NumPayments=Periods;
var Prin=PresentAmount;
MonthPayment=Math.floor((Prin)*(Math.pow((1+AnnualInterestRate),(Periods)))*100)/100;
FVFactor=(Math.pow((1+AnnualInterestRate),(Periods)))
return MonthPayment
}
回答by Ashutosh Pandit
This is my way of writing code for compound interest
这是我编写复利代码的方式
function call()
{
var A = Principle;
var B = Interest;
var C = Years;
var D = 0;
var E = A*B/100;
D+=E;
var f=E+A;
document.write("0 year: Interest "+E+" Principal: "+f);
document.write("<br />");
for (var i=1; i<C; i++)
{
E=f*B/100;
D+=E;
f=E+f;
document.write(i+"year: Interest "+E+" Principal:"+f);
document.write("<br />");
}
return false;
}
回答by Fernando Palma
Also I found with this alternative:
我还发现了这个替代方案:
function compoundInterest(principal, annual_rate, n_times, t_years) {
return principal*(Math.pow(1 + annual_rate/n_times, n_times*t_years) - 1);
}

