Calculate the present value of a future amount of money in C#

This example shows how to calculate the present value of a future amount of money.

Suppose you want a certain amount of money in a given number of years and you know you can realize a specific interest rate (compounded annually). (Yes, I know you can’t really predict interest rates.) In that case, what amount of money would you need to invest now to achieve your goal? The result is called the “present value” or “present discounted value” of the future amount.

The formula for calculating the future value FV of an investment of PV (present value) now at interest rate I for T years is given by the formula:

`FV = PV * (1 + I)T`

The example Calculate compound interest over time in C# performs this calculation.

Solving this equation for present value PV gives:

`PV = FV / (1 + I)T`

It’s that simple. This program uses the following code to calculate the present value.

```// Calculate the present value.
private void btnCalculate_Click(object sender, EventArgs e)
{
txtPresentValue.Clear();

// Get the inputs.
decimal interest_rate =
decimal.Parse(txtInterestRate.Text.Replace("%", ""));
if (txtInterestRate.Text.Contains("%"))
interest_rate /= 100;

decimal future_value =
decimal.Parse(txtAmount.Text, NumberStyles.Any);

decimal years = decimal.Parse(txtYears.Text);

// Calculate and display the result.
decimal current_value =
(decimal)future_value /
(decimal)Math.Pow((double)(1 + interest_rate), (double)years);
txtPresentValue.Text = current_value.ToString("C");
}```

The program gets the input values, converting the interest rate from a percent to a decimal if necessary and allowing currency symbols in the future amount. It then uses the formula to calculate the result and displays it as a currency value.

This entry was posted in algorithms, mathematics and tagged , , , , , , , , , , . Bookmark the permalink.