Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PMT function in Javascript

Tags:

I want to use Excel PMT function in Javascript. The parameter would be

Pmt( interest_rate, number_payments, PV, FV, Type )

interest_rate : the interest rate for the loan.
number_payments : the number of payments for the loan.
PV : the present value or principal of the loan.
FV : It is the future value or the loan amount outstanding after all payments have been made. 

Type is : It indicates when the payments are due. Type can be one of the following values:
 0, 1

You can refer : http://www.techonthenet.com/excel/formulas/pmt.php

this is the code I use, I am stuck in last parameter. Which is "type is" 0 or 1. How it effect the calculations please.

function PMT (ir, np, pv, fv ) {
 /*
 ir - interest rate per month
 np - number of periods (months)
 pv - present value
 fv - future value (residual value)
 */
 pmt = ( ir * ( pv * Math.pow ( (ir+1), np ) + fv ) ) / ( ( ir + 1 ) * ( Math.pow ( (ir+1), np) -1 ) );
 return pmt;
}

I need it in plain Javascript and not in jQuery please.

like image 651
dps123 Avatar asked Mar 14 '11 02:03

dps123


People also ask

What is the PMT function?

PMT, one of the financial functions, calculates the payment for a loan based on constant payments and a constant interest rate. Use the Excel Formula Coach to figure out a monthly loan payment. At the same time, you'll learn how to use the PMT function in a formula.

What are the 3 arguments needed for the PMT function?

The PMT function uses the following arguments: Rate (required argument) – The interest rate of the loan. Nper (required argument) – Total number of payments for the loan taken. Pv (required argument) – The present value or total amount that a series of future payments is worth now.

What are the parameters of PMT function?

The PMT function uses the following arguments: Rate (required argument) – The interest rate of the loan. Nper (required argument) – Total number of payments for the loan taken. Pv (required argument) – The present value or total amount that a series of future payments is worth now.


2 Answers

this is my version of PMT function after some googling:

function PMT(ir, np, pv, fv, type) {
    /*
     * ir   - interest rate per month
     * np   - number of periods (months)
     * pv   - present value
     * fv   - future value
     * type - when the payments are due:
     *        0: end of the period, e.g. end of month (default)
     *        1: beginning of period
     */
    var pmt, pvif;

    fv || (fv = 0);
    type || (type = 0);

    if (ir === 0)
        return -(pv + fv)/np;

    pvif = Math.pow(1 + ir, np);
    pmt = - ir * (pv * pvif + fv) / (pvif - 1);

    if (type === 1)
        pmt /= (1 + ir);

    return pmt;
}

Example What is the monthly payment needed to pay off a $200,000 loan in 15 years at an annual interest rate of 7.5%?

ir = 0.075 / 12
np = 15 * 12
pv = 200000
pmt = PMT(ir, np, pv).toFixed(2) = -1854.02
payoff = pmt * np = -333723.6
like image 63
vault Avatar answered Sep 20 '22 03:09

vault


here in my PMT version

PMT: function(rate, nperiod, pv, fv, type) {
    if (!fv) fv = 0;
    if (!type) type = 0;

    if (rate == 0) return -(pv + fv)/nperiod;

    var pvif = Math.pow(1 + rate, nperiod);
    var pmt = rate / (pvif - 1) * -(pv * pvif + fv);

    if (type == 1) {
        pmt /= (1 + rate);
    };

    return pmt;
},

//// Call the PMT

 var result = PMT(6.5/1200 , 30*12 , 65000 , 0 , 0);
 console.log(result);
 //// result : -410.8442152704279

/// Other as well IPMT and PPMT

 IPMT: function(pv, pmt, rate, per) {
    var tmp = Math.pow(1 + rate, per);
    return 0 - (pv * tmp * rate + pmt * (tmp - 1));
},

PPMT: function(rate, per, nper, pv, fv, type) {
    if (per < 1 || (per >= nper + 1)) return null;
    var pmt = this.PMT(rate, nper, pv, fv, type);
    var ipmt = this.IPMT(pv, pmt, rate, per - 1);
    return pmt - ipmt;
},
like image 32
M Arfan Avatar answered Sep 20 '22 03:09

M Arfan