Home > Articles > Web Development > Ajax and JavaScript

Performing Financial Calculations

Paul McFedries
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close WindowPaul McFedries

Paul McFedries

Learn more…

Tweaking the Windows 7 Registry
Jul 17, 2009
Running Scripts to Enhance and Improve Windows
Jun 11, 2009
Building a Budget PC
Jun 16, 2008
Accessing and Sharing Network Resources in Windows Vista
May 28, 2008
Programming the Windows Script Host
Apr 29, 2008
Tricks for Working with Word 2007 Documents
Mar 31, 2008
Building Basic Formulas in Excel 2007
Mar 26, 2008
Collaborating with Windows Meeting Space
Feb 15, 2008
Troubleshooting Tools Included with Windows Vista
Feb 15, 2008
Using Virtual Private Network Connections in Windows Vista
Feb 15, 2008
Implementing Group Policies with Windows Vista
Feb 8, 2008
Programming the Windows Script Host
Feb 8, 2008
Windows Vista's Stability Improvements
Feb 8, 2008
Adding Devices to the Windows Home Server Network
Sep 21, 2007
Understanding Program Variables in VBA
Jun 15, 2007
Designing Microsoft Access 2007 Forms for Efficient and Accurate Data Entry
May 4, 2007
Getting the Most Out of Windows Vista User Accounts
Mar 9, 2007
The Windows Vista Interface
Oct 6, 2006
Mastering File Types in Windows XP
Nov 4, 2005
Constructing Knockout Presentations in PowerPoint
Apr 22, 2005
Working with Multiple-Table Queries in Access
Sep 10, 2004
Building Basic Formulas in Excel
Aug 27, 2004
Understanding Program Variables in VBA
Apr 2, 2004
From Buck-Naked to Beautiful: Dressing Up Your Page
Jun 7, 2002
Performing Financial Calculations
Sep 2, 2001
Creating and Using Cookies
Sep 2, 2001
Encrypting Text with JavaScript
Sep 2, 2001
Making Form Fields Mandatory
Sep 2, 2001
Cross-Browser Solutions: Detection and Generic Functions
Sep 2, 2001
Understanding Variables in JavaScript
Jul 6, 2001
Mac OS X Snow Leopard In DepthMac OS X Snow Leopard In Depth
Sep 22, 2009
Mac OS X Snow Leopard In Depth, Adobe ReaderMac OS X Snow Leopard In Depth, Adobe Reader
Sep 15, 2009
Microsoft Windows 7 UnleashedMicrosoft Windows 7 Unleashed
Jul 14, 2009
Microsoft Windows 7 Unleashed, Adobe ReaderMicrosoft Windows 7 Unleashed, Adobe Reader
Jul 8, 2009
Tweak It and Freak It: A Killer Guide to Making Windows Run Your WayTweak It and Freak It: A Killer Guide to Making Windows Run Your Way
Mar 27, 2009
Tweak It and Freak It: A Killer Guide to Making Windows Run Your Way, Adobe ReaderTweak It and Freak It: A Killer Guide to Making Windows Run Your Way, Adobe Reader
Mar 18, 2009
Build It. Fix It. Own It: A Beginner's Guide to Building and Upgrading a PCBuild It. Fix It. Own It: A Beginner's Guide to Building and Upgrading a PC
May 19, 2008
Build It. Fix It. Own It: A Beginner's Guide to Building and Upgrading a PC, Adobe ReaderBuild It. Fix It. Own It: A Beginner's Guide to Building and Upgrading a PC, Adobe Reader
May 7, 2008
Microsoft Windows Vista Unleashed, 2nd EditionMicrosoft Windows Vista Unleashed, 2nd Edition
Apr 4, 2008
Microsoft Windows Vista Unleashed, Adobe Reader, 2nd EditionMicrosoft Windows Vista Unleashed, Adobe Reader, 2nd Edition
Apr 4, 2008
Networking with Microsoft Windows Vista: Your Guide to Easy and Secure Windows Vista NetworkingNetworking with Microsoft Windows Vista: Your Guide to Easy and Secure Windows Vista Networking
Dec 19, 2007
Networking with Microsoft Windows Vista: Your Guide to Easy and Secure Windows Vista Networking (Adobe Reader)Networking with Microsoft Windows Vista: Your Guide to Easy and Secure Windows Vista Networking (Adobe Reader)
Dec 19, 2007
Microsoft Windows Home Server UnleashedMicrosoft Windows Home Server Unleashed
Sep 13, 2007
Microsoft Windows Home Server Unleashed (Adobe Reader)Microsoft Windows Home Server Unleashed (Adobe Reader)
Sep 13, 2007
Tricks of the Microsoft Office 2007 GurusTricks of the Microsoft Office 2007 Gurus
May 16, 2007
Tricks of the Microsoft Office 2007 Gurus (Adobe Reader), 2nd EditionTricks of the Microsoft Office 2007 Gurus (Adobe Reader), 2nd Edition
May 16, 2007
Microsoft Office Access 2007 Forms, Reports, and QueriesMicrosoft Office Access 2007 Forms, Reports, and Queries
May 1, 2007
Microsoft Office Access 2007 Forms, Reports, and Queries (Adobe Reader)Microsoft Office Access 2007 Forms, Reports, and Queries (Adobe Reader)
May 1, 2007
VBA for the 2007 Microsoft Office SystemVBA for the 2007 Microsoft Office System
Mar 30, 2007
VBA for the 2007 Microsoft Office System (Adobe Reader)VBA for the 2007 Microsoft Office System (Adobe Reader)
Mar 30, 2007
Formulas and Functions with Microsoft Office Excel 2007Formulas and Functions with Microsoft Office Excel 2007
Mar 14, 2007
Formulas and Functions with Microsoft Office Excel 2007 (Adobe Reader)Formulas and Functions with Microsoft Office Excel 2007 (Adobe Reader)
Mar 14, 2007
Microsoft Windows Vista UnleashedMicrosoft Windows Vista Unleashed
Dec 8, 2006
Microsoft Windows Vista Unleashed, Adobe ReaderMicrosoft Windows Vista Unleashed, Adobe Reader
Dec 8, 2006
Microsoft Windows Vista UnveiledMicrosoft Windows Vista Unveiled
Jun 9, 2006
Microsoft Windows Vista Unveiled, Adobe ReaderMicrosoft Windows Vista Unveiled, Adobe Reader
Jun 9, 2006
Special Edition Using JavaScript, Adobe ReaderSpecial Edition Using JavaScript, Adobe Reader
Feb 14, 2006
Microsoft Windows XP UnleashedMicrosoft Windows XP Unleashed
Sep 6, 2005
Microsoft Windows XP Unleashed, Adobe ReaderMicrosoft Windows XP Unleashed, Adobe Reader
Sep 6, 2005
Tricks of the Microsoft Office GurusTricks of the Microsoft Office Gurus
Apr 12, 2005
Microsoft Access 2003 Forms, Reports, and QueriesMicrosoft Access 2003 Forms, Reports, and Queries
Aug 18, 2004
Microsoft Access 2003 Forms, Reports, and Queries, Adobe ReaderMicrosoft Access 2003 Forms, Reports, and Queries, Adobe Reader
Aug 18, 2004
Formulas and Functions with Microsoft Excel 2003Formulas and Functions with Microsoft Excel 2003
Jun 11, 2004
Formulas and Functions with Microsoft Excel 2003, Adobe ReaderFormulas and Functions with Microsoft Excel 2003, Adobe Reader
Jun 11, 2004
Absolute Beginner's Guide to VBAAbsolute Beginner's Guide to VBA
Mar 9, 2004
Absolute Beginner's Guide to VBA, Adobe ReaderAbsolute Beginner's Guide to VBA, Adobe Reader
Mar 9, 2004
Unauthorized Guide to Windows Millennium, TheUnauthorized Guide to Windows Millennium, The
Sep 20, 2000
The Unauthorized Guide to Windows 98, 2nd EditionThe Unauthorized Guide to Windows 98, 2nd Edition
Aug 28, 1999

Sorry, this author hasn't posted any blogs.

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.

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.

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Informit Network