Home > Articles > Web Development > Ajax and JavaScript

  • Print
  • + Share This
From the author of

Taking Inflation into Account

When discussing the future value of an investment, it's always wise to take inflation into account. Even a low rate of 1% or 2% can erode the value of money over a long period.

To take inflation into account, use the following general formula:

PV = FV / (1 + IR)NP

Here, IR is the inflation rate and NP is the number of years into the future that you want to check. Here's the JavaScript version:

PV = FV / (Math.pow(1 + IR, NP)

Listing 5 calculates the present value of $500,000 in 20 years, assuming a 2% inflation rate.

Listing 5: Taking Inflation into Account

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

function inflation_factor(FV, IR, NP) {
  var PV = FV / (Math.pow(1 + IR, NP))
  return round_decimals(PV, 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 future_value = 500000
var inflation_rate = 0.02
var years_from_now = 20
var adjusted_value = inflation_factor(future_value, inflation_rate, 
                   years_from_now)

alert("Future value:\t$" + future_value + "\n" +
   "Inflation rate:\t" + inflation_rate * 100 + "%\n" +
   "Years from now:\t" + years_from_now + " years\n\n" +
   "Adjusted value:\t$" + adjusted_value)

//-->
</script>
  • + Share This
  • 🔖 Save To Your Account