views:

246

answers:

4

Present Value is the value on a given date of a future payment or series of future payments, discounted to reflect the time value of money and other factors such as investment risk. And yes, I know it's formula so don't need that.

But I am wondering if this method is already predefined within the .NET libraries. I can't find it. (And I guess .NET doesn't contain this function so I will just have to use my own function instead.)

I have no use for any nice third-party components or samples written in C#. I have to use some third-party scripting solution which allows any standard .NET library. I can add custom references to third-party libraries and even write my own add-on modules, but am trying to avoid this.

+2  A: 

The visual basic library has a Financial class in it. Financial.PV() should do the trick for you.

If you're not using Visual Basic - you can still reference that library from your project in other languages and use the financial functions.

Per your comment that you can't use the library directly, the visual basic source code inside the Financial.Pv function looks something like this...

Public Shared Function PV(ByVal Rate As Double, ByVal NPer As Double, ByVal Pmt As Double, ByVal Optional FV As Double = 0, ByVal Optional Due As DueDate = 0) As Double
    Dim num As Double
    If (Rate = 0) Then
        Return (-FV - (Pmt * NPer))
    End If
    If (Due <> DueDate.EndOfPeriod) Then
        num = (1 + Rate)
    Else
        num = 1
    End If
    Dim x As Double = (1 + Rate)
    Dim num2 As Double = Math.Pow(x, NPer)
    Return (-(FV + ((Pmt * num) * ((num2 - 1) / Rate))) / num2)
End Function
Scott Ivey
I'm not even any Microsoft IDE. Am using a third-party tool called "Composer" which is a semi-graphical development system. It creates .NET assemblies that can be added to existing C# projects. It's mostly for non-technical people to write simple formula's. Unfortunately, it doesn't support the VisualBasic assemblies.
Workshop Alex
You could use reflector on the visual basic assemblies to grab their formulas and use them directly in your project.
Scott Ivey
+4  A: 

It is, in type Microsoft.VisualBasic.Financial, Microsoft.VisualBasic.dll the static PV function computes the Present Value.

Also worth noting are the Excel Financial Functions implemented in F#. This API has the option to be used with out any F# design or runtime components.

Scott Weinstein
+1 for mentioning Excel Financial Functions.
Noldorin
+1  A: 

Beware. You need to distinguish between continuously compounding e.g.

Pv = exp(-rt).Fv

and discretely compounding e.g

Pv = Fv.[(1 + r)^(-n)]

(the above is for annual payment)

These will give different answers and you need to know which one to use.

Brian Agnew
The calculation is doing monthly payments. The periods are in number of months and the interest is the monthly interest.
Workshop Alex
So you can adapt the second formula appropriately. That's fine. So long as you know what's required (some people don't, and it all ends in tears :-)
Brian Agnew
+3  A: 

You could use the Microsoft.VisualBasic library, but this doesn't seem like a very elegant solution to me. I wouldn't be surprised if it were deprecated in the future anyway.

A simple extension would be a nice solution:

public decimal PresentValue(this decimal value, decimal interestRate, 
    decimal years)
{
    return value / Math.Pow(1 + interestRate, years);
}

This is of course based off the simple definition of compound interest. There are variations on the present value which you could calculate similarly.

Note the use of the decimal type to preserve base 10 accuracy. It also allows a fractional time length by using Math.Pow.

Noldorin