Visual C#

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…

2 min read519 words

Introduction#

Percentages are used in statistics and algebra to represent values as fractions between 0 and 1, often written as x%. You will encounter percentage values in several different scenarios:

  • 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

Hands-on Exercise: Building the Application

  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click File -> New Project...
  3. In the middle list, click Windows Forms Application
  4. Change the Name to PercentageConversions
  5. Click OK
  6. 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
  7. Double-click the first = button
  8. 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"); }
  9. Return to the form
  10. Double-click the second = button
  11. 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"); }
  12. Return to the form
  13. Double-click the third = button
  14. 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"); }
  15. Return to the form
  16. Double-click the last = button
  17. 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"); }
  18. Return to the form
  19. Double-click the Close button
  20. Implement its event as follows: private void btnClose_Click(object sender, EventArgs e) { Close(); }
  21. To execute, press F5
  22. Enter some values and click the = buttons
  23. Close the form and return to the your programming environment

Home Copyright © 2010-2011 C# Key