Hi,
I have a program that calculates the average of three test scores. The test scores are entered into text boxes, I convert the text to uint and the result into a double. I then am converting the final result back into a string to display in a label. How can I format the string output so it will show a two point decimal? For example: (10 + 20 + 2)/3 = 10.66666 I would like to show 10.66.
Here is what I have, I am just not quite sure how the syntax would be and where I would put it.
Thanks in advance for your help.
//sets clear and exit buttons to visible on calculate button click event
uint score1 = new uint();
uint score2 = new uint();
uint score3 = new uint();
double result1 = new double();
double result2 = new double();
btnclear.Visible = true;
btnexit.Visible = true;
try
{
//convert input values to numeric and assign to variables.
score1 = uint.Parse(txtscore1.Text);
score2 = uint.Parse(txtscore2.Text);
score3 = uint.Parse(txtscore3.Text);
}
catch (FormatException)
{
// Display error messagebox should test scores entered not be numeric
MessageBox.Show("Please enter three whole number, numeric scores. Press clear to begin again.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
//Sum then average the three test scores.
result1 = score1 + score2 + score3;
result2 = result1 / 3;
// Sets output labels to visible, formats and displays output.
lbloutput1.Visible = true;
lbloutput2.Visible = true;
lbloutput2.Text = result2.ToString();

1 answers
To display 10.66 (i.e. round down to 2 dp), you could use:
To round to the nearer second place (i.e. 10.67), just use:
lbloutput2.Text = result2.ToString("F2");answered 2 years ago by:
17279
78
Once again, Thank you so much vulpes. You explained the things I have asked so clearly. If you are not a teacher you should be. :)