我想在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 pls。提前致谢
理解Type参数影响的最简单方法是尝试以下值:年利率= 12%,月数= 1,当前值= 100
当Type = 0(默认值)时,PMT()函数将产生101
当Type = 1时,PMT()函数将产生100
当Type = 0时,利息计算为1个月,因为假定付款是在月末。对于Type = 1,利息计算为0个月,因为付款是在月初。
一些谷歌搜索后,这是我的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
这是我的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;
},
我使用以下功能解决了这个问题,感谢网站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);
}
或者如果你有,就像我们的情况一样,是年度报价
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);
}