Link to home
Start Free TrialLog in
Avatar of Hojoformo
Hojoformo

asked on

Net Present Value Function

Is there a math function in C# for computing the Net Present Value?  
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sumix
sumix


There is a .net class you can use, Microsoft.VisualBasic.Financial, which has a static method named NPV.
ok, sorry for my double posting.
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