Javascript 中的 PMT 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5294074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:31:50  来源:igfitidea点击:

PMT function in Javascript

javascriptjavascript-framework

提问by dps123

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

我想在 Javascript 中使用 Excel PMT 函数。该参数将是

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

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

你可以参考: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.

这是我使用的代码,我被困在最后一个参数中。哪个是“类型是”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;
}

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

我需要它在纯 Javascript 中,而不是在 jQuery 中。

采纳答案by Nik Kalyani

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

了解 Type 参数影响的最简单方法是尝试以下值:Annual Interest = 12%, # of Months = 1, Present Value = 100

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

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

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

当 Type=1 时,PMT() 函数将产生 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.

Type=0 时,利息计算为 1 个月,因为假定付款是在月底。对于 Type=1,利息计算为 0 个月,因为付款是在月初。

回答by vault

this is my version of PMT function after some googling:

这是我在谷歌搜索后的 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;
}

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

示例以 7.5% 的年利率在 15 年内还清 200,000 美元的贷款所需的每月付款是多少?

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

回答by M Arfan

here in my PMT version

在我的 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;
},

//// Call the PMT

/// 调用 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和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;
},

回答by Tobias Lanz

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

感谢网站tvmcalcs.comAn Example with Advance Payments,我使用以下函数解决了这个问题。

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

or if you have, as in our case, a 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);
}