Solaris
Solaris

Reputation: 1

How can I use Convert.ToSingle so it can give me a decimal number?

[My executable][1]: https://i.sstatic.net/jt4AK.png

So, I'm creating an executable program that will give me my GPA based on two grades (exam 1 + exam 2). My code is running with Convert.ToSingle but the grades are not showing up as decimals, and I'm always getting numbers like 8,575 or 9,765. Is there a way that I can round up these numbers? (ex: 8,6 or 9,8).

private void button1_Click(object sender, EventArgs e) // Calcular
{
    float soma, media, valor;
    int peso = 7;
    soma = 0;

        foreach(Control controle in this.Controls)
    {
        if(controle is TextBox)
        {
            valor = Convert.ToSingle(((TextBox)controle).Text);
           
            soma += valor*peso;
            peso = 3;
            
        }
        

        media = soma / 10;
        this.Controls["label5"].Text = media.ToString();


    }
}
 

Also, I tried to solve my problem using Math.Round but I'm starting to code and perhaps I'm not using it right. But when I try to use it, I can't run my code. It shows error CS0149 - Method Name Expected.

That's how I've tried to run it (with Math.Round):


      private void button1_Click(object sender, EventArgs e) // Calcular
  {
      float soma, media, valor;
      int peso = 7;
      soma = 0;

          foreach(Control controle in this.Controls)
      {
          if(controle is TextBox)
          {
              valor = Math.Round(Convert.ToDecimal(((media)controle).Text), 1);
             
              soma += valor*peso;
              peso = 3;
              
          }
          

          media = soma / 10;
          this.Controls["label5"].Text = media.ToString();


      }
  }

Is there a way that I can have decimals using Convert.ToSingle?

Thank you!

Upvotes: 0

Views: 28

Answers (0)

Related Questions