Javascript 复利计算器不能产生正确的值

Javascript 复利计算器不能产生正确的值,javascript,math,chartkick,Javascript,Math,Chartkick,我正在用javascript创建一个复利公式,其中包含每月供款,用于填充chartkick.js图形,以显示每年年底本金增长的总额。这是我遵循的公式 Compound Interest For Principal P(1 + r/n) ^ nt Future Value of a Series PMT * (((1 + r/n)^nt - 1) / (r/n)) * (1+r/n) A = [Compound Interest for Principal] + [Future Value o

我正在用javascript创建一个复利公式,其中包含每月供款,用于填充
chartkick.js
图形,以显示每年年底本金增长的总额。这是我遵循的公式

Compound Interest For Principal
P(1 + r/n) ^ nt
Future Value of a Series
PMT * (((1 + r/n)^nt - 1) / (r/n)) * (1+r/n) 

A = [Compound Interest for Principal] + [Future Value of a Series]

A = the future value of the investment/loan, including interest
P = the principal investment amount (the initial deposit or loan amount)
PMT = the monthly payment
r = the annual interest rate (decimal)
n = the number of times that interest is compounded per year AND additional payment frequency
t = the number of years the money is invested or borrowed for 
*/
以下是我的javascript:

P = $("#start-amount").val().replace(/[^\d\.]/g, '');
var t = $("#years").val(); 
var PMT = $("#contributions-amount").val().replace(/[^\d\.]/g, '');  
var n = $("#contribution-rate").val();
//If you choose monthly it is equal to 12 and annually it is equal to 1 
//Convert Interest Rate to Decimal
var r_percentage = $("#interest-rate").val().replace(/[^\d\.]/g, '');
var r = parseFloat(r_percentage) /  100.0;

//Create an arary with list of numbers less than number of years
var t_arr = [];
for (i = 0; i <= t; i++) {
    if (i > 0 && i <= t) {
        t_arr.push(i)
    }
}

//For Chartkick Chart
data = [
{"name": "With interest", "data": {}
},
{"name": "No Interest", "data": {}
}];

/* 
Needs to be like this!
data = [
  {"name":"Workout", "data": {"2013-02-10 00:00:00 -0800": 3, "2013-02-17 00:00:00 -0800": 4}},
  {"name":"Call parents", "data": {"2013-02-10 00:00:00 -0800": 5, "2013-02-17 00:00:00 -0800": 3}}
];*/

/*With Interest
J is equal to each individual year less than the total number of years*/
for (j = 1; j <= t_arr.length; j++) {
    var compound_interest_for_principal = P * (Math.pow(1 + r / n, n * t));
    var value_rounded1 = Math.round(compound_interest_for_principal * 100) /100;
    var future_value_of_series = PMT * (Math.pow(1 + r / n, n * j) - 1) / (r / n) * (1 + r / n);
    var value_rounded2 = Math.round(future_value_of_series * 100) / 100;
    var A = value_rounded1 + value_rounded2;
    var A = A.toFixed(2);
    if (data[0]["data"][j] === undefined) {
        data[0]["data"][j] = A;
    }
}

/*Without Interest */
for (k = 1; k <= t_arr.length; k++) {
    var r = 0;
    var principal_no_interest= P;
    var value_rounded1 = Math.round(principal_no_interest * 100) /100;
    var monthly_no_interest = PMT * n * k;
    var value_rounded2 = Math.round(monthly_no_interest * 100) / 100;
    var A = value_rounded1 + value_rounded2;
    var A = A.toFixed(2);
    if (data[1]["data"][k] === undefined) {
        data[1]["data"][k] = A;
    }
}
new Chartkick.LineChart("savings-chart", data, {"discrete": true});
P=$(“#起始金额”).val().替换(/[^\d\.]/g,”);
var t=$(“#年”).val();
var PMT=$(“#供款金额”).val().替换(/[^\d\.]/g,”);
var n=$(“#贡献率”).val();
//如果选择每月,则等于12;如果选择每年,则等于1
//将利率转换成十进制
var r#u percentage=$(“#利率”).val()。替换(/[^\d\.]/g,”);
var r=浮动(r_百分比)/100.0;
//创建一个arary,其数字列表少于年数
var t_arr=[];
对于(i=0;i0&&i您可以检查此项

var result = PMT * (Math.pow(1 + r / n, nt) - 1) / (r / n) * (1 + r / n);

谢谢大家的帮助。我找到了一个解决方案。以下是错误代码与正确代码的对比:

/*
Formula to Implement
P(1 + r/n) ^ nt
Future Value of a Series
PMT * (((1 + r/n)^nt - 1) / (r/n)) * (1+r/n) 

A = [Compound Interest for Principal] + [Future Value of a Series]

A = the future value of the investment/loan, including interest
P = the principal investment amount (the initial deposit or loan amount)
PMT = the monthly payment
r = the annual interest rate (decimal)
n = the number of times that interest is compounded per year AND additional payment frequency
t = the number of years the money is invested or borrowed for 
/*

/*INCORRECT SOLUTION
J is equal to each individual year less than the total number of years*/
for (j = 1; j <= t_arr.length; j++) {
    //Correct
    var compound_interest_for_principal = P * (Math.pow(1 + r / n, n * t));
    //Correct
    var a= Math.round(compound_interest_for_principal * 100) /100;
    //Incorrect
    var future_value_of_series = PMT * (Math.pow(1 + r / n, n * j) - 1) / (r / n) * (1 + r / n);
    var b = Math.round(future_value_of_series * 100) / 100;
    var A = a + b;
    var A = A.toFixed(2);
    if (data[0]["data"][j] === undefined) {
        data[0]["data"][j] = A;
    }
}

//CORRECT SOLUTION
for (j = 1; j <= t_arr.length; j++) {
    //Incorrect
    var compound_interest_for_principal = P * (Math.pow(1 + r / n, n * j));
    var a = Math.round(compound_interest_for_principal * 100) / 100;
    var future_value_of_series = ((PMT * n) / r) * (Math.pow(1 + r / n, n * j) - 1) 
    var b = Math.round(future_value_of_series * 100) / 100;
    var A = a + b
    var A = A.toFixed(2);
    if (data[0]["data"][j] === undefined) {
        data[0]["data"][j] = A;
}
/*
实施方案
P(1+r/n)^nt
序列的未来值
付款*((1+r/n)^nt-1)/(r/n))*(1+r/n)
A=[本金复利]+[系列的未来价值]
A=投资/贷款的未来价值,包括利息
P=本金投资金额(初始存款或贷款金额)
PMT=每月付款
r=年利率(十进制)
n=每年复利的次数和额外支付频率
t=投资或借款的年数
/*
/*不正确的解决方案
J等于每一年少于总年数*/

对于(j=1;j)用
Math.pow(x,y)
替换任何
x^y
。有什么问题吗?问题是将公式的前半部分嵌套在那里……答案将以百万计。不会。你保留嵌套,因此
Math.pow(x,y)
中的
x
(1+r/n)
…问题可能在于
var未来价值=供款金额*((1+利率/供款利率)^(供款利率*年数)-1/(利率/年数)*(1+(利率/年数))末尾缺少一个结束括号;
@JasonBourne IEEE 754可以处理高达1.7976931348623157e+308的双倍。有什么问题吗?我试过了,但它给了我负值。如果你想看的话,我刚刚用完整的代码更新了我的第一篇文章。@JasonBourne,我现在使用了原始公式。