-1

now I have 3 textboxes that the user can type in them his degrees and the average will be calculated and displayed in another textbox! I've made the result to show just two fractional digits by calling the Math.Round() Method This is my code:

double Sum = int.Parse(textBox1.Text) + int.Parse(textBox2.Text) + int.Parse(textBox3.Text);
double Avg =  Sum / 3;
textBox4.Text = Math.Round(Avg, 2).ToString();

My problem is whenever the average is equal to an integer number like 20, I want it to display 20.00

1 Answers1

2

Since C# 6.0 you can use string interpolation to format variables into a string (in this case, format the number with two decimal places):

$"{Avg:.00}"

Alternatively, use string#Format:

string.Format("{0:.00}", Avg);

If you don't want to use either of those, you can use the ToString function with this parameter for that as mentioned in the comments:

Avg.ToString("0.00")
nbokmans
  • 5,492
  • 4
  • 35
  • 59