Skip to content Skip to sidebar Skip to footer

Pmt Function In Javascript

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_

Solution 1:

this is my version of PMT function after some googling:

functionPMT(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 / 12np = 15 * 12pv = 200000pmt = PMT(ir, np, pv).toFixed(2) = -1854.02payoff = pmt * np = -333723.6

Solution 2:

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);
    return0 - (pv * tmp * rate + pmt * (tmp - 1));
},

PPMT: function(rate, per, nper, pv, fv, type) {
    if (per < 1 || (per >= nper + 1)) returnnull;
    var pmt = this.PMT(rate, nper, pv, fv, type);
    var ipmt = this.IPMT(pv, pmt, rate, per - 1);
    return pmt - ipmt;
},

Solution 3:

The easiest way to understand the impact of the Type parameter is to try the following values: Annual Interest = 12%, # of Months = 1, Present Value = 100

When Type=0 (the default), the PMT() function will yield 101

When Type=1, the PMT() function will yield 100

With Type=0, the interest is computed for 1 month because the payment is assumed to be at the end of the month. For Type=1, the interest is computed for 0 months because the payment is at the beginning of the month.

Solution 4:

I solved it using the following function, thanks to the site tvmcalcs.com, An Example with Advance Payments.

functionpmt(monthlyRate, monthlyPayments, presentValue, residualValue, advancedPayments) {
        t1 = 1+monthlyRate
        t2 = Math.pow(t1,monthlyPayments)
        t3 = Math.pow(t1,(monthlyPayments-advancedPayments))
        return (presentValue-(residualValue/t2))/(((1-(1/(t3)))/monthlyRate)+advancedPayments);
    }

or if you have, as in our case, a annualRate

functionpmtWithAnnualRate(annualRate, monthlyPayments, presentValue, residualValue, advancedPayments) {
    monthlyRate = annualRate / 1200
    t1 = 1 + monthlyRate
    t2 = Math.pow(t1,monthlyPayments)
    t3 = Math.pow(t1,(monthlyPayments-advancedPayments))
    return (presentValue-(residualValue/t2))/(((1-(1/(t3)))/monthlyRate)+advancedPayments);
}

Solution 5:

PMT - (Pmt is the periodical amount to be paid / received) function in javascript

varAnualRate=0.05; //5% interste ratevarNumberOfYear=10;
varNumberOfPayments= NumberOfYear * 12;
varByYear=12;
varLoanAmount=20000;
varFutureValue=0;

varPresentValueInterstFector= Math.pow((1 + AnualRate/ByYear), NumberOfPayments);
varPeriodicPayment=  (AnualRate/ByYear) * LoanAmount  * (PresentValueInterstFector + FutureValue)/(PresentValueInterstFector-1); 

varTotalCost= PeriodicPayment * NumberOfPayments;
varTotalInterset= TotalCost - LoanAmount; 

console.log("TotalCost ", TotalCost);
console.log("TotalInterset ", TotalInterset);

using javascript function

var interestRate = 0.05; // or (5/100) this rate is annualvar numberofYear = 10;
var numberofPayment = numberofYear * 12; 
var presentValue = 20000; //this is loan functionPMT(ir,np, pv, fv = 0){ 
 var presentValueInterstFector = Math.pow((1 + ir), np);
 var pmt = ir * pv  * (presentValueInterstFector + fv)/(PresentValueInterstFector-1); 
 return pmt;
}

var testPMT = PMT(interestRate/12, numberofPayment, presentValue); //output console.log(testPMT); 

Time Value of Money calculator

functioncalculate(){

 var getRate          = parseFloat(document.getElementById('rate').value);
 var getNumberOfYears = parseInt(document.getElementById('numOfYears').value);
 var getNumOfPayments = parseInt(document.getElementById('numOfPayments').value);
 var getLoanAmount    = parseFloat(document.getElementById('loanAmount').value);

 var pmt = PMT(getRate/12, getNumOfPayments, getLoanAmount);
 document.getElementById('disMonthlypayment').innerHTML = pmt;

 var totalCost = pmt * getNumOfPayments;
 document.getElementById('disTotalcost').innerHTML = totalCost;

 var totalInterset = totalCost - getLoanAmount;
 document.getElementById('disTotalInterst').innerHTML = totalInterset;

}

 //PMT function (Fazlan refer this)functionPMT(ir,np, pv, fv = 0){ 
 // ir: interest rate// np: number of payment// pv: present value or loan amount// fv: future value. default is 0var presentValueInterstFector = Math.pow((1 + ir), np);
 var pmt = ir * pv  * (presentValueInterstFector + fv)/(presentValueInterstFector-1); 
 return pmt;
}
<table><tr><td>Rate               : </td><td><inputtype="text"id="rate"placeholder="0.05" />  ex: 0.05 = (5/100) </td></tr><tr><td>Number of Years    : </td><td><inputtype="text"id="numOfYears"placeholder="10" /></td></tr><tr><td>Number of Payments : </td><td><inputtype="text"id="numOfPayments"placeholder="120" />   Number of Years * 12  </td></tr><tr><td>Loan Amount        : </td><td><inputtype="text"id="loanAmount"placeholder="20000" /></td></tr><tr><td>Monthly Payment : </td><td><bid="disMonthlypayment"></b></td></tr><tr><td>Total Cost      : </td><td><bid="disTotalcost"></b></td></tr><tr><td>Total Interest  : </td><td><bid="disTotalInterst"></b></td></tr><tr><td><buttononclick="calculate()">Calculate</button></td></tr></table>

Post a Comment for "Pmt Function In Javascript"