使用JavaScript计算复合兴趣的未来值 [英] Calculating future value with compound interest with JavaScript

查看:115
本文介绍了使用JavaScript计算复合兴趣的未来值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,用户在该脚本中插入月收入,并在30年后获得复合利息的未来值。就像现在一样,我已经为测试目的分配了一些值。

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.

// 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);
}

问题是,我实际上是在使用Excel的Excel电子表格中构建这个内置的FV()公式,当交叉检查时,我的结果完全关闭......任何想法我做错了,因为我根本没有进入金融数学。在此先感谢。

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.

推荐答案

Math.pow 是不必要的,因为您正逐月计算并递增 futureValue 。只需乘以 1 + monthlyRate 即可。您还希望在乘以之前将投资的当前值添加到新投资中:

The Math.pow is unnecessary, since you are calculating and incrementing futureValue month by month. Simply multiply by 1 + monthlyRate. You also want to add the current value of the investment to the new investment before multiplying:

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;

这篇关于使用JavaScript计算复合兴趣的未来值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆