Home > Articles > Web Development > Ajax and JavaScript

Performing Financial Calculations

Paul McFedries teaches you how to use JavaScript to perform a number of basic financial calculations, including loan or mortgage payments, the future value of an investment, and inflation.
This article is excerpted from Special Edition Using JavaScript.
Like this article? We recommend

Like this article? We recommend

JavaScript doesn't have any built-in financial functions for calculating things like loan payments and future values. However, the formulas for many financial calculations combine the four basic arithmetic operations—addition, subtraction, multiplication, and division—with exponentiation, raising one number to the power of another. The latter is handled by the Math.pow() method:

Math.pow(Number1, Number2)

Number1

The base number

Number2

The exponent (that is, the power to which Number1 is raised)


For example, Math.pow(4, 2) raises 4 to the power of 2, which gives 16.

In the sections that follow, I'll show you how to use JavaScript to perform a number of basic financial calculations. Along the way, I'll provide the general formulas for each calculation, and these formulas will use the terms in Table 1.

Table 1 Terms Used in the Financial Formulas

Term

What It Means

FV

The future value of an investment

IR

The interest rate for an investment or loan

NP

The number of periods in an investment or loan

PMT

The regular payment into an investment or loan

PV

The present value of an investment or loan


Calculating Loan or Mortgage Payments

Loans and mortgages are a fact of financial life, and it's a rare household that doesn't have or need a loan of some kind. An easy way to add some value to your site is to set up a kind of loan or mortgage calculator where users can enter the specifics of the loan and your script returns the monthly payment.

Here's the general formula for this kind of calculation:

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

Here's how this looks as a JavaScript expression:

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

Listing 1 puts this expression through its paces.

Listing 1: Calculating a Loan Payment

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

function calculate_payment(PV, IR, NP) {
  var PMT = (PV * IR) / (1 - Math.pow(1 + IR, -NP))
  return round_decimals(PMT, 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 present_value = 200000
var interest_rate = 0.09
var loan_term = 30
var monthly_payment = calculate_payment(present_value, 
[ic:ccc]interest_rate / 12, loan_term * 12)

alert("Amount of the loan:\t$" + present_value + "\n" +
   "Annual interest rate:\t" + interest_rate * 100 + "%\n" +
   "Term of the loan:\t" + loan_term + " years\n\n" +
   "Monthly payment:\t$" + monthly_payment)

//-->
</script>

The function calculate_payment() does the heavy lifting here. Notice how it takes as arguments the three terms required by the expression: PV, IR, and NP. The loan payment result is stored in the PMT variable, which is then sent to the round decimals() function to round it to two decimal places.

In the setup for the function, the variables present_value, interest_rate, and loan_term are initialized and then passed to the calculate_payment() function.

NOTE

When the values are passed to the calculate_payment() function, notice that the interest_rate is divided by 12, and loan_term is multiplied by 12. You need to do this because the original values supplied to these variables are annual values, so they must be converted into monthly numbers before getting to the function.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.