6

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.

TruMan1
  • 33,665
  • 59
  • 184
  • 335
  • What error you get on compiling? – STO Jun 29 '10 at 18:27
  • 2
    .NET 3.5's C# doesn't support optional parameters so you will need to specify the two optional params too. In any case, don't you need to take a twelvth root to work out the monthly interest that'll compound to a given annual rate, not just divide by 12? – Rup Jun 29 '10 at 18:40
  • You can use RedGate Reflector to decompile the function into C# from the Microsoft.VisualBasic assembly. It's fairly simple - a few ifs and a few calculations - but I'm not sure it's ethical for someone to copy / paste it here for you. – Rup Jun 29 '10 at 18:46

4 Answers4

14

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.

Community
  • 1
  • 1
MarkJ
  • 30,070
  • 5
  • 68
  • 111
4

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
stack247
  • 5,579
  • 4
  • 41
  • 64
  • if i try PMT(7, 360, 120000) in excel it returns 840,000. is there another step involved to get correct value? – Haris Feb 02 '16 at 11:50
  • Excel's `PMT` first parameter (`rate`) is written as a decimal number and monthly rate. So, from the first and second examples, the 'correct' Excel formulas are: `=PMT(0.07/12, 360, 120000)` and `=PMT(0.045/12, 360, 137500.47)`, respectively. – stack247 Feb 04 '16 at 00:57
  • 2
    :-) Actually Microsoft.VisualBasic.dll is written in C# :-) http://referencesource.microsoft.com/#Microsoft.VisualBasic/Microsoft/VisualBasic/Financial.cs – Markus Feb 05 '16 at 21:10
2

As already stated you can use the Microsoft.VisualBasic assembly which provides plenty of the VB6 functionality. But to be honest if you are looking more generally at financial calculations you should consider looking at Excel Financial Functions for .NET.

Update: The library can be installed via NuGet

PM> Install-Package ExcelFinancialFunctions

Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
  • 5
    That's a one-man-band unsupported library from Code Galleries. I'd need a strong argument to prefer using that over a fully supported core library from the .NET framework, whatever DLL it lives in. Are there many financial calcs in that library that aren't in Microsoft.VisualBasic? – MarkJ Jun 30 '10 at 11:35
  • @NoChance, that blog post seems to refer to the same library as linked in the answer. The link to the library is dead, I updated the link in the answer to point to the GitHub repository for the project. I hope that helps. – Chris Taylor Aug 04 '19 at 17:58
  • 1
    Thank you for following up. I noticed that the library link was changed. Interested readers may also take a look at the blog:https://ricardocovo.com/2013/01/14/financial-functions-in-net-c/ – NoChance Aug 04 '19 at 21:52
1

You must add a reference to the Microsoft.VisualBasic.dll assembly for your project in Visual Studio. This is not the same thing as the using Microsoft.VisualBasic; directive at the top your source file. You must do both steps.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794