Home > Articles > Web Development > Ajax and JavaScript

  • Print
  • + Share This
From the author of

Calculating the Future Value of an Investment

Whether it's for the kids' education or for a retirement, investors need to know how much their investments will be worth in the future. In other words, if a person deposits PMT dollars into an investment earning an interest rate IR for NP years, what's the future value? Here's the formula that figures this out:

FV = PMT x ((1 + IR)NP – 1) / IR

Here's the corresponding JavaScript expression:

FV = PMT * (Math.pow(1 + IR, NP) - 1) / IR

Listing 3 tries it out using an annual payment of $1,000, an annual interest rate of 8%, and a term of 20 years.

Listing 3: Calculating the Future Value of an Investment

<script language="JavaScript" type="text/javascript">
<!--

function future_value(PMT, IR, NP) {
  var FV = PMT * (Math.pow(1 + IR, NP) - 1) / IR
  return round_decimals(FV, 2)
}

function round_decimals(original_number, decimals) {
  var result1 = original_number * Math.pow(10, decimals)
  var result2 = Math.round(result1)
  var result3 = result2 / Math.pow(10, decimals)
  return (result3)
}

var annual_payment = 1000
var interest_rate = 0.08
var investment_term = 20
var investment_value = future_value(annual_payment, interest_rate, 
                  investment_term)

alert("Annual payment:\t$" + annual_payment + "\n" +
   "Annual interest rate:\t" + interest_rate * 100 + "%\n" +
   "Investment term:\t" + investment_term + " years\n\n" +
   "Future value:\t$" + investment_value)

//-->
</script>

This example assumes annual payments. If you want to calculate the future value based on monthly payments, don't forget to multiply the number of years by 12 to get the total number of months (NP) and to divide the annual interest rate by 12 to get the monthly rate (IR).

  • + Share This
  • 🔖 Save To Your Account