Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make it, so there are only 3 or 4 number after a comma in C#?

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading