Common Javascript Functions for Financial Calculations


This article provides some Javascript functions for common financial calculations. It can be easily adapted to other programming languages as well, because the formulas themselves are not complex at all.

Present Value

1
2
3
4
5
6
7
8
9
10
11
12
13
 /***********************************************
  *              Present Value                  *
  * pv = fv / (1 + (rate / freq))^periods       *
  * pv = Present Value                          *
  * fv = Future Value                           *
  * rate = interest rate (expressed as %)       *
  * freq = compounding frequency                *
  * periods = number of periods until maturity  *
  ***********************************************/
function presentValue(fv, freq, rate, periods)
{
    return (fv / Math.pow((1 + (rate / 100 / freq)), periods));
}
 /***********************************************
  *              Present Value                  *
  * pv = fv / (1 + (rate / freq))^periods       *
  * pv = Present Value                          *
  * fv = Future Value                           *
  * rate = interest rate (expressed as %)       *
  * freq = compounding frequency                *
  * periods = number of periods until maturity  *
  ***********************************************/
function presentValue(fv, freq, rate, periods)
{
	return (fv / Math.pow((1 + (rate / 100 / freq)), periods));
}

Future Value

1
2
3
4
5
6
7
8
9
10
11
12
13
 /************************************************
  *                Future Value                  *
  * fv = pv * (1 + (rate / freq))^periods        *
  * fv = Future Value                            *
  * pv = Present Value                           *
  * rate = interest rate (expressed as %)        *
  * freq = compounding frequency                 *
  * periods = number of periods until maturity   *
  ************************************************/
function futureValue(pv, freq, rate, periods)
{
    return (pv * Math.pow(1 + (rate / 100 / freq), periods));
}
 /************************************************
  *                Future Value                  *
  * fv = pv * (1 + (rate / freq))^periods        *
  * fv = Future Value                            *
  * pv = Present Value                           *
  * rate = interest rate (expressed as %)        *
  * freq = compounding frequency                 *
  * periods = number of periods until maturity   *
  ************************************************/
function futureValue(pv, freq, rate, periods)
{
	return (pv * Math.pow(1 + (rate / 100 / freq), periods));
}

Annualized Return

1
2
3
4
5
6
7
8
9
10
11
 /************************************************
  *            Annualized Return                 *
  * r = (fv - pv) / pv / years                   *
  * fv = future value                            *
  * pv = present value                           *
  * years = term of loan in years                *
  ************************************************/
function annualizedReturn(fv, pv, years)
{
    return (fv - pv) / pv / years;
}
 /************************************************
  *            Annualized Return                 *
  * r = (fv - pv) / pv / years                   *
  * fv = future value                            *
  * pv = present value                           *
  * years = term of loan in years                *
  ************************************************/
function annualizedReturn(fv, pv, years)
{
	return (fv - pv) / pv / years;
}

Monthly Payment

1
2
3
4
5
6
7
function monthlyPayment(pv, freq, rate, periods)
{
    rate = rate / 100 / freq;
    
    var x = Math.pow(1 + rate, periods);
    return (pv * x * rate) / (x - 1);
}
function monthlyPayment(pv, freq, rate, periods)
{
	rate = rate / 100 / freq;
	
	var x = Math.pow(1 + rate, periods);
	return (pv * x * rate) / (x - 1);
}

Annuity

1
2
3
4
5
6
7
8
9
10
11
12
13
 /***********************************************
  *                 Annuity                     *
  * a = fv / (((1 + r / c)^n) - 1) / (r/c)      *
  * fv = future value                           *
  * r = interest rate                           *
  * c = compounding frequency                   *
  * n = total number of periods                 *
  ***********************************************/  
function annuity(fv, freq, rate, periods)
{
    rate = rate / 100 / freq;
    return (fv / ((Math.pow(1 + rate, periods) - 1)) * rate);
}
 /***********************************************
  *                 Annuity                     *
  * a = fv / (((1 + r / c)^n) - 1) / (r/c)      *
  * fv = future value                           *
  * r = interest rate                           *
  * c = compounding frequency                   *
  * n = total number of periods                 *
  ***********************************************/  
function annuity(fv, freq, rate, periods)
{
	rate = rate / 100 / freq;
	return (fv / ((Math.pow(1 + rate, periods) - 1)) * rate);
}

Mortgage Principal

1
2
3
4
5
function calcAmortPrincipal(pymt, freq, rate, periods)
{
    rate = rate / 100 / freq;
    return (pymt * (1 - (1 / Math.pow(1 + rate, periods))) / rate);
}
function calcAmortPrincipal(pymt, freq, rate, periods)
{
	rate = rate / 100 / freq;
	return (pymt * (1 - (1 / Math.pow(1 + rate, periods))) / rate);
}

Regular Deposit

1
2
3
4
5
function regularDeposit(payment, freq, rate, periods)
{
    rate = rate / 100 / freq;
    return (payment * (Math.pow(1 + rate, periods) - 1) / rate * (1 + rate));
}
function regularDeposit(payment, freq, rate, periods)
{
	rate = rate / 100 / freq;
	return (payment * (Math.pow(1 + rate, periods) - 1) / rate * (1 + rate));
}

Currency Format

In finance, the money is represented using two fixed decimal places, and the numbers are separated by commas every three positions. So the following function can be useful in converting float numbers into currency format (nicely printable format).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 /***********************************************
  *  Convert to currency notation               *
  ***********************************************/
function toCurrency(num) {
    num = Math.round(num * 100) * 0.01;
    var currstring = num.toString();
    if (currstring.match(/\./)) {
        var curr = currstring.split('.');
    } else {
        var curr = [currstring, "00"];
    }
    curr[1] += "00";
    curr[2] = "";
    var returnval = "";
    var length = curr[0].length;
    
    // add 0 to decimal if necessary
    for (var i = 0; i < 2; i++) 
        curr[2] += curr[1].substr(i, 1);
 
    // insert commas for readability
    for (i = length; (i - 3) > 0; i = i - 3) {
        returnval = "," + curr[0].substr(i - 3, 3) + returnval;
    }
    returnval = curr[0].substr(0, i) + returnval + "." + curr[2];
    return(returnval);
}
 /***********************************************
  *  Convert to currency notation               *
  ***********************************************/
function toCurrency(num) {
	num = Math.round(num * 100) * 0.01;
	var currstring = num.toString();
	if (currstring.match(/\./)) {
		var curr = currstring.split('.');
	} else {
		var curr = [currstring, "00"];
	}
	curr[1] += "00";
	curr[2] = "";
	var returnval = "";
	var length = curr[0].length;
	
	// add 0 to decimal if necessary
	for (var i = 0; i < 2; i++) 
		curr[2] += curr[1].substr(i, 1);

	// insert commas for readability
	for (i = length; (i - 3) > 0; i = i - 3) {
		returnval = "," + curr[0].substr(i - 3, 3) + returnval;
	}
	returnval = curr[0].substr(0, i) + returnval + "." + curr[2];
	return(returnval);
}

Example toCurrency(12345678.32342) gives 12,345,678.32

The above all functions can be downloaded at https://helloacm.com/js/finance.js. Use it for free but at your own risk!

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
505 words
Last Post: Showing Uptime of Server Status on Webpages using PHP and Crontab
Next Post: How to Cache Heavy Operations (such as Database Queries) in PHP?

The Permanent URL is: Common Javascript Functions for Financial Calculations

Leave a Reply