我真的在为一个看起来应该相当简单的计算而苦苦挣扎...
我想计算初始投资的复利,每月供款...
这是我目前所拥有的:
function makeInvestingCalculation() {
let princ = 3500; // start deposit
let add = 250; // monthly deposit (need plus it every year)
let rate = 12 / 100; // interest rate divided to create decimal
let months = (10 * 12); //10 years of monthly contributions
for (let i = 0; i < months; i++) {
if (i != 0) {
princ += add;
}
princ += princ * rate;
}
console.log(princ.toFixed(2)); //4498379090.49
}
makeInvestingCalculation();
这产生了显然不正确的 4498379090.49
。
正确答案应该是 69,636.12
但我找不到这个...
任何帮助将不胜感激。
答案 0 :(得分:1)
主要问题是(正如@CertainPerformance 正确指出的那样)
princ += princ * rate;
由于您按月计算复利,您需要将年利率除以 12(月),所以
princ += princ * (rate/12);
第二个更微妙的问题是
for (let i = 0; i < months; i++) {
if (i != 0) {
princ += add;
}
您正在跳过第一笔付款,因为您在第 0 个月没有做出贡献,所以直觉上可能会进行付款,但是,由于循环条件是 < months
而不是 <= months
,您是 < em>也跳过最后一个贡献,所以你的循环会提前停止 1 个贡献。从概念上讲,循环条件对我来说更有意义,因为:
for(let i=1; i <= 12*10; i++)
那么
let princ = 3500; // start deposit
let add = 250; // monthly deposit (need plus it every year)
let rate = 12 / 100; // interest rate divided to create decimal
let months = (10 * 12); //10 years of monthly contributions
for (let i = 1; i <= months; i++) {
princ += add;
princ += princ * (rate / 12);
console.log(princ);
}
console.log(princ.toFixed(2)); //69636.12