 |
Microsoft Visual C# Example Application: Percentage
Conversions |
|
|
In mathematics and related subjects such as algebra or
statistics, a percentage is used to express a value that is a fraction
between 0 and 1. It is sometimes expressed as x%.
|
There are various scenarios for which you deal with
percent values:
- Sometimes you get a decimal value that is between 0 and 1, such as
0.86 or 0.1245, and you need its percentage equivalent
- Sometimes you are given a fraction, such as 3/8 and must know how
must that value represents in percent
- Sometimes you have a percent value but must convert it to decimal
|
|
Application:
Creating the Application
|
|
- Start Microsoft Visual Studio
- To create a new application, on the main menu, click File -> New
Project...
- In the middle list, click Windows Forms Application
- Change the Name to PercentageConversions
- Click OK
- Design the form as follows:
 |
Control |
Text |
Name |
TextAlign |
GroupBox |
 |
Converting a Percentage to Decimal |
|
|
Label |
 |
Value: |
|
|
TextBox |
 |
5 |
txtPercentage1 |
Right |
Label |
 |
% of |
|
|
TextBox |
 |
10 |
txtValue1 |
Right |
Button |
 |
= |
btnConvert1 |
|
TextBox |
 |
|
txtValue1 |
Right |
GroupBox |
 |
Converting a Fraction to a Percentage |
|
|
TextBox |
 |
3 |
txtNumerator |
Right |
Label |
 |
/ |
|
|
TextBox |
 |
4 |
txtDenominator |
Right |
Button |
 |
= |
btnFractionToPercent |
|
TextBox |
 |
|
txtPercentage2 |
Right |
Label |
 |
% |
|
|
GroupBox |
 |
Converting a Decimal to Percentage |
|
|
Label |
 |
Value: |
|
|
TextBox |
 |
0.125 |
txtDecimal1 |
Right |
Button |
 |
= |
btnDecimalToPercent |
|
TextBox |
 |
|
txtPercentage3 |
Right |
Label |
 |
% |
|
|
GroupBox |
 |
Converting a Percentage to Decimal |
|
|
Label |
 |
Value: |
|
|
TextBox |
 |
75 |
txtPercentage4 |
Right |
Label |
 |
% |
|
|
Button |
 |
= |
btnPercentToDecimal |
|
TextBox |
 |
|
txtDecimal2 |
Right |
Button |
 |
Close |
btnClose |
|
|
- Double-click the first = button
- Implement its event as follows:
private void btnConvert1_Click_1(object sender, EventArgs e)
{
double percent = 0.00;
double value = 0.00;
double result;
try
{
percent = double.Parse(txtPercentage1.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Percent Value", "Percentage Conversion");
}
try
{
value = double.Parse(txtValue1.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Value to Convert", "Percentage Conversion");
}
result = percent * value / 100;
txtResult1.Text = result.ToString("F");
}
- Return to the form
- Double-click the second = button
- Implement its event as follows:
private void btnFractionToPercent_Click(object sender, EventArgs e)
{
double numerator = 0.00;
double denominator = 0.00;
double result;
try
{
numerator = double.Parse(txtNumerator.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Numerator", "Percentage Conversion");
}
try
{
denominator = double.Parse(txtDenominator.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Value to Convert", "Percentage Conversion");
}
result = (numerator / denominator) * 100;
txtPercentage2.Text = result.ToString("F");
}
- Return to the form
- Double-click the third = button
- Implement its event as follows:
private void btnDecimalToPercent_Click(object sender, EventArgs e)
{
double value = 0.00;
double result;
try
{
value = double.Parse(txtDecimal1.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Value to Convert", "Percentage Conversion");
}
result = value * 100;
txtPercentage3.Text = result.ToString("F");
}
- Return to the form
- Double-click the last = button
- Implement its event as follows:
private void btnPercentToDecimal_Click(object sender, EventArgs e)
{
double value = 0.00;
double result;
try
{
value = double.Parse(txtPercentage4.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Value to Convert", "Percentage Conversion");
}
result = value / 100;
txtDecimal2.Text = result.ToString("F");
}
- Return to the form
- Double-click the Close button
- Implement its event as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- To execute, press F5

- Enter some values and click the = buttons


- Close the form and return to the your programming environment
Application
|
|