Square root C #

1

Good, After the user tips I decided to improve my C # code on the calculator. I think it is simpler but I have a small problem in Square Root because I try to make the count and the result always gives me 0.

    private void btn_raiz_Click(object sender, RoutedEventArgs e)
    {
        resutado = Convert.ToSingle(Math.Sqrt(primeiro));

        janela.Text = resutado.ToString();

    }
    
asked by anonymous 23.06.2016 / 17:42

2 answers

1

May be the type of variables you are using.

protected void Button1_Click(object sender, EventArgs e)
    {
        double primeiro = 2;

        double resutado = Convert.ToSingle(Math.Sqrt(primeiro));

        janela.Text = resutado.ToString();

    }

I developed this function and it worked perfectly.

    
23.06.2016 / 20:16
0

I do not understand why this Convert.ToSingle .

Faster alternative (not tested):

private void btn_raiz_Click(object sender, RoutedEventArgs e)
{
    resutado = (float)Math.Sqrt(primeiro); //A variável "resultado" está declarada como float? Se não, vai dar problema...
    janela.Text = resutado.ToString();
}
    
24.06.2016 / 02:34