0

I am new in programming completely. This is my first lesson. I know 0%. We are trying OOP Windows Form app. SO how can I make it so there is only 3/4 numbers after the comma. The Code:

double TgG(double x)
{
    return Math.Tan(x*Math.PI/180);
} //TanG

private void button1_Click(object sender, EventArgs e)
{
    double x, y;
    x= double.Parse(textBox1.Text);
    y = TgG(x);
    label2.Text = y.ToString();
} //TanG(x) button click
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • 1
    Taker a look at the documentation for the double.ToString() method you use. You should find an overload there for that method to allow formatting the output. – Ralf Sep 15 '22 at 15:23
  • See also this one https://stackoverflow.com/questions/3602392/round-double-to-two-decimal-places – Stefan Wuebbe Sep 15 '22 at 15:35

2 Answers2

1

In C# you choose the format when you dispaly a value or convert it to a string. To do that use this overload of ToString with the following format:

// Format as a numeric value with 3 decimal places.
label2.Text = y.ToString("N3");

The documentation linked above gives several other built-in formats, and you can specify custom formats as well.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

Use the .Round() method. Here's an example of using it:

    //Rounding 1.678 to 2 decimal places
    Math.Round(1.678, 2); //1.68