Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB "Financial.Pmt" equivalent in C#?

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, dLoanAmount) * -1

My current project is in C# and I need to use this function. Answers on the web say just add the namespace and assembly and use the same in C#- but this is not true! C# still does not recognize this formula.

So how can I use use Financial.Pmt in C# (or perhaps even porting the source code to it)? Thanks for any help.

like image 624
TruMan1 Avatar asked Jun 29 '10 18:06

TruMan1


2 Answers

Like this:

using System;
using Microsoft.VisualBasic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            double dAPR = 2;
            Int32 iNumberOfPayments = 12;
            double dLoanAmount = 10000;
            Console.WriteLine(Financial.Pmt((dAPR / 100) / 12, iNumberOfPayments, dLoanAmount, 0, DueDate.EndOfPeriod) * -1);
            Console.ReadLine();
        }
    }
}
  • Like Joel says, add a reference to the Microsoft.VisualBasic assembly.
  • Like Rup says in a comment, you have to provide the defaults to the 4th and 5th arguments.

Do use Microsoft.VisualBasic from C# when appropriate. It's a fully supported core library in .Net and it contains some useful financial functions.

like image 156
MarkJ Avatar answered Sep 17 '22 15:09

MarkJ


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
like image 44
stack247 Avatar answered Sep 16 '22 15:09

stack247