Present value calculations using c sharp (c#).
How much do I need to invest today in order to have $1,000,000 in the future? To answer this question, you also need to know how long you are willing to leave your money invested and what the interest rate you will be paid.
Take the following equation that computes simple interest for you:
A = P(1 + i) ^ n
Where:
A = Amount
P = Principal/Present Value Invested
i = interest
n = number of years
With this formula, you could find out how much the $500 you invest in your 4.6% CD will be worth in 2 years ($547). We can shift variables on opposite sides of the equal sign and isolate P to determine how much we need to invest to have x amount of dollars in the future.
New equation:
P = A / (1 + i) ^ n
Or
P = A(1 + i) ^ -n [The rules for a negative exponent]
The program below is written in C# and illustrates the use of the latter equation above, but can also be switched to the former by replacing the comments. It is my preference to avoid using division when programming equations (simple and complex ones). Programming languages sometimes differ in their methods of applying the order of operations and the way they read equations (e.g. Parenthesis, exponents, multiplication, division, etc … and left to right, right to left).
The program below provides examples of the following topics in c# (csharp)
1.) Changing label font color
2.) Mouse Hovers and Mouse Leave actions on windows forms
3.) Use of Pow method from Math Class
4.) Text formatting on label
To download the source code, click on the attachment link at the bottom of this page.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace PresentValueCalculator
{
public partial class Form1 : Form
{
//At the bottom of the program is a default message
//that instructs the user to hover over the labels
//to find out about the numbers that are to be typed
//in the text boxes. This is the default message.
string defaultMsg =
"Hover the your cursor over any \n" +
"of the labels next to text boxes. \n" +
"This will provide a brief summary \n" +
"of the value that you are being \n" +
"asked to enter. ";
public Form1()
{
InitializeComponent();
//Populate the bottom label with the default message
lblMessageArea.Text = defaultMsg;
}
private void lblFutureAmount_MouseHover(object sender, EventArgs e)
{
//Change the message text and font color to blue when
//the mouse is hovered over the label.
//This is done for all labels with a corresponding textbox
lblMessageArea.ForeColor = System.Drawing.Color.Blue;
lblMessageArea.Text =
"Place the lump sum amount of \n" +
"money that you wish to have at \n" +
"some point in the future. \n" +
"Do not enter any characters \n" +
"other than numbers. ";
}
private void label_MouseLeave(object sender, EventArgs e)
{
lblMessageArea.ForeColor = System.Drawing.Color.Black;
lblMessageArea.Text = defaultMsg;
}
private void lblInterestRate_MouseHover(object sender, EventArgs e)
{
lblMessageArea.ForeColor = System.Drawing.Color.Blue;
lblMessageArea.Text =
"Enter the interest rate you \n" +
"anticipate to earn on your \n" +
"investment. \n" +
"Do not enter any characters \n" +
"other than numbers. ";
}
private void lblYearsInvested_MouseHover(object sender, EventArgs e)
{
lblMessageArea.ForeColor = System.Drawing.Color.Blue;
lblMessageArea.Text =
"Enter the amount of years\n" +
"you plan on leaving your \n" +
"investment working for you. \n" +
"Do not enter any characters \n" +
"other than numbers. ";
}
private void btnCalculate_Click(object sender, EventArgs e)
{
//We want to use the Pow method in the math class
//so in addition to the obvious variables,
//we want to also add a final rate, which
//represents the interest rate after modifications
double futureAmt,
interestRate,
years,
finalRate,
presentValue;
futureAmt = Convert.ToDouble(txtFutureValue.Text);
interestRate = Convert.ToDouble(txtInterestRate.Text);
years = Convert.ToDouble(txtYears.Text);
finalRate = 1 + (interestRate/100);
presentValue = futureAmt * Math.Pow(finalRate, -years);
//alternatively, you can also write
//presentValue = futureAmt / Math.Pow(finalRate, years);
lblPresentValue.Text = String.Format("{0:c}",presentValue);
}
}
}
| Attachment | Size |
|---|---|
| PresentValueCalculator.zip | 43.24 KB |