Javascript中的PMT功能

dps*_*123 10 javascript 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
Run Code Online (Sandbox Code Playgroud)

您可以参考: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;
}
Run Code Online (Sandbox Code Playgroud)

我需要它在普通的js而不是jquery pls.提前致谢

vau*_*ult 31

一些谷歌搜索后,这是我的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;
}
Run Code Online (Sandbox Code Playgroud)

我用它像:

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

  • @vault,fv值计算不正确,因为pv * (pvif + fv)应该是(pv * pvif + fv) (3认同)

M A*_*fan 9

这是我的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;
},
Run Code Online (Sandbox Code Playgroud)

////打电话给PMT

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

///其他以及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;
},
Run Code Online (Sandbox Code Playgroud)


Nik*_*ani 4

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

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

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

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