How to Show a split result on the screen

Asked

Viewed 58 times

1

I am developing a project in which the result of the division of the 5 items always 0, someone can help me to tidy up

private void btnCalcu_Click(object sender, EventArgs e)
    {
        //convertendo
        Total = Convert.ToInt32(txtFechado.Text); 
        SemInt = Convert.ToInt32(txtSemInt.Text);
        LigDepois = Convert.ToInt32(txtLigaDps.Text);
        Negoc = Convert.ToInt32(txtNegoc.Text);
        Agend = Convert.ToInt32(txtAgend.Text);

        //Calculando

        totalLig = Total + SemInt + LigDepois + Negoc + Agend;
        porc = (Total/100) * totalLig;
        TotPoc = Convert.ToDouble(porc);
        MessageBox.Show("Total de ligações é: " + totalLig + " Pocentual é : " +TotPoc );



    }
  • This will not be related to the values you are using in the calculation?

  • @Joãomartins, the TotPoc no, but the totalLig yes that is strange, I do not know if I am wrong in the logic of the account or what

  • Edit your question and put the example values you are using, maybe this way we can come up with a solution?

1 answer

1

When you make a division between two integers (int), the result will be integer..

ex: 8 / 10 = 0

if you want 0.8 you will have to convert one of the values to double, or another type that accepts decimal values..

ex:

double TotPoc = ((double)Total/100) * totalLig;

Since you use the 100 can use the suffix D

would look like this:

  double TotPoc = (Total/100D) * totalLig;

Another thing would be when receiving values from the user would be to use the int.Tryparse instead of Convert.ToInt32

Browser other questions tagged

You are not signed in. Login or sign up in order to post.