Javascript中的PMT功能

时间:2011-03-14 02:14:19

标签: javascript javascript-events javascript-framework

我想在Javascript中使用Excel PMT功能。 参数是

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

你可以参考: http://www.techonthenet.com/excel/formulas/pmt.php

这是我使用的代码,我被困在最后一个参数中。这是“类型是”0或1.请如何影响计算。

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;
}

我需要在普通的js中而不是在jquery中。 提前致谢

5 个答案:

答案 0 :(得分:26)

这是我在一些谷歌搜索后的PMT功能版本:

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;
}

示例 15年内以每年7.5%的利率偿还20万美元贷款所需的每月还款额是多少?

ir = 0.075 / 12
np = 15 * 12
pv = 200000
pmt = PMT(ir, np, pv).toFixed(2) = -1854.02
payoff = pmt * np = -333723.6

答案 1 :(得分:8)

这里是我的PMT版本

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;
},

////拨打PMT

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

///其他以及IPMT和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;
},

答案 2 :(得分:3)

了解Type参数影响的最简单方法是尝试以下值:年利率= 12%,月数= 1,现值= 100

当Type = 0(默认值)时,PMT()函数将产生101

当Type = 1时,PMT()函数将产生100

如果Type = 0,则计算利息为1个月,因为假定付款是在月末。对于Type = 1,利息计算为0个月,因为付款是在月初。

答案 3 :(得分:1)

感谢网站tvmcalcs.com预付款示例,我使用以下功能解决了该问题。

function pmt(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);
    }

或者,如果您拥有我们的情况下的AnnualRate

function pmtWithAnnualRate(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);
}

答案 4 :(得分:1)

PMT-(PMT是要支付/收取的定期金额)JavaScript中的功能

var AnualRate = 0.05; //5% interste rate
var NumberOfYear = 10;
var NumberOfPayments = NumberOfYear * 12;
var ByYear = 12;
var LoanAmount = 20000;
var FutureValue = 0;

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

var TotalCost = PeriodicPayment * NumberOfPayments;
var TotalInterset = TotalCost - LoanAmount; 

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

使用javascript函数

var interestRate = 0.05; // or (5/100) this rate is annual
var numberofYear = 10;
var numberofPayment = numberofYear * 12; 
var presentValue = 20000; //this is loan 

function PMT(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

function calculate(){

 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)

function PMT(ir,np, pv, fv = 0){ 
 // ir: interest rate
 // np: number of payment
 // pv: present value or loan amount
 // fv: future value. default is 0

 var presentValueInterstFector = Math.pow((1 + ir), np);
 var pmt = ir * pv  * (presentValueInterstFector + fv)/(presentValueInterstFector-1); 
 return pmt;
}
<table>
<tr><td>Rate               : </td><td> <input type="text" id="rate"          placeholder="0.05" />  ex: 0.05 = (5/100) </td></tr>
<tr><td>Number of Years    : </td><td> <input type="text" id="numOfYears"    placeholder="10" />    </td></tr>
<tr><td>Number of Payments : </td><td> <input type="text" id="numOfPayments" placeholder="120" />   Number of Years * 12  </td></tr>
<tr><td>Loan Amount        : </td><td> <input type="text" id="loanAmount"    placeholder="20000" /> </td></tr>

<tr><td>Monthly Payment : </td><td> <b id="disMonthlypayment"></b> </td></tr>
<tr><td>Total Cost      : </td><td> <b id="disTotalcost"></b>      </td></tr>
<tr><td>Total Interest  : </td><td> <b id="disTotalInterst"></b>   </td></tr>

<tr><td><button onclick="calculate()">Calculate</button></td></tr>

</table>