PMT function Payment Type

陌路散爱 提交于 2019-12-06 21:50:40

I am not sure what you need the PaymentType for, but here what I use for PMT function in C#, it's pure C# PMT function:

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

Usage:

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

Using @stack247's answer, if there is a chance the yearly interest rate can be 0% then you might want to add this:

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;
}
user11969472
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));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!