PMT功能支付类型

时间:2011-03-18 12:47:09

标签: c# javascript vb.net javascript-events javascript-framework

以下是我计算贷款支付的功能, 和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功能帮助我修改上述功能。

http://www.techonthenet.com/excel/formulas/pmt.php

提前致谢

3 个答案:

答案 0 :(得分:4)

我不确定你需要什么样的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

答案 1 :(得分:0)

使用@ stack247的答案,如果有机会将年利率设置为0%,则可能需要添加以下内容:

private decimal Pmt(double yearlyInterestRate, int totalNumberOfMonths, double loanAmount)
{
    if (yearlyInterestRate > 0)
    {
        var rate = (double) yearlyInterestRate / 100 / 12;
        var denominator = Math.Pow((1 + rate), totalNumberOfMonths) - 1;
        return new decimal((rate + (rate / denominator)) * loanAmount);
    }
    return totalNumberOfMonths > 0 ? new decimal(loanAmount / totalNumberOfMonths) : 0;
}

答案 2 :(得分:-2)

public static double PMT(double RATE,  int NPER, int PV, long FV, int TYPE)
{
    return -RATE * (FV + PV * Math.Pow(1 + RATE, NPER)) / ((Math.Pow(1 + RATE, NPER) - 1) * (1 + RATE * TYPE));
}