VB “Financial.Pmt” equivalent in C#?

前端 未结 4 1086
深忆病人
深忆病人 2021-01-13 23:30

There is a built in function from the Microsoft.VisualBasic assembly. I can use it in VB like this:

Financial.Pmt((dAPR / 100) / 12, iNumberOfPayments, dLoan         


        
4条回答
  •  离开以前
    2021-01-14 00:19

    For those who don't like to import VB functions. Here's pure C# code for 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;
    }
    

    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
    

提交回复
热议问题