[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1177
  • Last Modified:

Net Present Value Function

Is there a math function in C# for computing the Net Present Value?  
0
Hojoformo
Asked:
Hojoformo
  • 2
1 Solution
 
anarki_jimbelCommented:
See Financial.NPV method (static)

It's in the Microsoft.VisualBasic namespace, however can be used in C#
0
 
sumixCommented:

There is a .net class you can use, Microsoft.VisualBasic.Financial, which has a static method named NPV.
0
 
sumixCommented:
ok, sorry for my double posting.
0
 
Fernando SotoCommented:
Hi Hojoformo;

Visual C# does not have the function but as the other post state you can import the function from Visual Basic. The function in C# would look like this:

    Microsoft.VisualBasic.Financial.NPV(FixedRetRate, ref values);

You must add a reference to the above function by going to the Windows Explorer and right click on References and select Add References and locate Microsoft.VisualBasic and add it to your project.

Here is some sample code to test the function.

            // Define money format.
            string MoneyFmt = "###,##0.00";
            // Define percentage format.
            string PercentFmt = "#0.00";

            double[] values = new double[5];
            // Business start-up costs.
            values[0] = -70000;
            // Positive cash flows reflecting income for four successive years.
            values[1] = 22000;
            values[2] = 25000;
            values[3] = 28000;
            values[4] = 31000;

            // Use the NPV function to calculate the net present value.
            // Set fixed internal rate.
            double FixedRetRate = 0.0625;
            // Calculate net present value.        
            double NetPVal = Microsoft.VisualBasic.Financial.NPV(
                FixedRetRate, ref values);
            string Msg = "The net present value of these cash flows is ";
            Msg = Msg + NetPVal.ToString(MoneyFmt);
            MessageBox.Show(Msg);       // Display net present value.

The above code came from Microsoft documentation at http://msdn2.microsoft.com/en-us/library/4k3y7xeh.aspx.

Fernando
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now