下面是我计算贷款支付的函数, 与在 Excel 中一样,我需要添加另一个参数,即支付类型。
function PMT (ir, np, pv, fv ) {
/*
ir - interest rate per month
np - number of periods (months)
pv - present value
fv - future value (residual value)
type - 0 or 1 need to implement that
*/
pmt = ( ir * ( pv * Math.pow ( (ir+1), np ) + fv ) ) / ( ( ir + 1 ) * ( Math.pow ( (ir+1), np) -1 ) );
return pmt;
}
如果 Type=0,则计算 1 个月的利息,因为假定付款是在月底。对于 Type=1,利息计算为 0 个月,因为付款是在月初。
谁能帮我用这个 PaymentType 特性修改上面的函数?
请您参考如下方法:
我不确定您需要 PaymentType 做什么,但这里我在 C# 中使用 PMT 函数,它是纯 C# PMT 函数:
public static double PMT(double yearlyInterestRate, int totalNumberOfMonths, double loanAmount)
{
var rate = (double) yearlyInterestRate / 100 / 12;
var denominator = Math.Pow((1 + rate), totalNumberOfMonths) - 1;
return (rate + (rate/denominator)) * loanAmount;
}
用法:
PMT(7, 360, 120000);
// Result: 798.36
PMT(4.5, 360, 137500.47);
// Result: 696.69
PMT(4.13, 360, 61520);
// Result: 298.33
PMT(6.38, 360, 89200);
// Result: 556.78
