how to perform percentage calculations in c #

0

I need a line of code that I want to use in the keypress event of a textbox.

You have to make when the user press enter perform:

txtcompra * txtganancia (%)+ txtcompra
txtventa.tostring();

the calculation would be something like that for more clarity:

precio de venta= precio de compra + (precio de compra * %de ganancia)

Current code:

database.SoloNumeros(e);
if (e.KeyChar == Convert.ToChar(Keys.Enter))
{
      float pventa, pcompra, ganancia;
      pcompra = float.Parse(txtcompra.Text);
      ganancia = float.Parse(txtganancia.Text);

      pventa = pcompra + pcompra * ganancia;
      txtventa.Text = pventa.ToString();
}
  

error: input string was not in the correct format

    
asked by Samuel Ignacio Susana Confesor 13.06.2017 в 16:16
source

4 answers

3

After reading your comments, it seems that you have several problems in your code.

On the one hand, in your TextBox ganancia you apparently have a symbol % . With that symbol you will not be able to convert the text to float , so you must delete it when passing it to the function Parse .

On the other hand, if the Culture configured in your system is Spanish, it will not recognize the point as a decimal separator (since in English the decimal separator is the , , the point is used as a thousand separator), so use CultureInfo.InvariantCulture for the point to be considered a decimal separator. So that line should look like this:

ganancia = float.Parse(txtganancia.Text
                     .Replace("%", ""), System.Globalization.CultureInfo.InvariantCulture);

It's also interesting to use TryParse instead of Parse , so you do not get an exception if you can not convert the text.

Finally, when it comes to working with currency, the appropriate type is never float , but decimal . If you do not use it, you can have several problems associated with floating point math.

With all these changes, your code should look like this:

database.SoloNumeros(e);
if (e.KeyChar == Convert.ToChar(Keys.Enter))
{
     decimal pventa, pcompra, ganancia;

     if (!decimal.TryParse(txtcompra.Text,
                           System.Globalization.NumberStyles.None, 
                           System.Globalization.CultureInfo.InvariantCulture, 
                           out pcompra))
     {
         MessageBox.Show("Error al convertir Precio de compra a decimal");
     }
     else
     {
         if (!decimal.TryParse(txtganancia.Text
                          .Replace("%", ""), 
                          System.Globalization.NumberStyles.None, 
                          System.Globalization.CultureInfo.InvariantCulture,
                          out ganancia))
         {
              MessageBox.Show("Error al convertir Porcentaje de ganancia a decimal");
         }
         else
         {
              pventa = pcompra * (1 +(ganancia/100));
              txtventa.Text = pventa.ToString();
         }
      }

}
    
answered by 13.06.2017 в 16:50
1

If what you want to control are errors due to incorrect formats, the code would look like this:

  private void txtventa_KeyPress(object sender, KeyPressEventArgs e)
    {

        if (e.KeyChar == Convert.ToChar(Keys.Enter))
        {
            float pventa, pcompra, ganancia;
            var s = txtcompra.Text.ToString(CultureInfo.InvariantCulture);
            if (!float.TryParse(txtcompra.Text.ToString(CultureInfo.InvariantCulture), out pcompra))
            {
                MessageBox.Show("Formato erroneo en TxtCompra");
                return;
            }
            if (!float.TryParse(txtganancia.Text.ToString(CultureInfo.InvariantCulture), out ganancia))
            {
                MessageBox.Show("Formato erroneo en txtganancia");
                return;
            }


            pventa = pcompra + pcompra * ganancia;
            txtventa.Text = pventa.ToString(CultureInfo.InvariantCulture);
        }
    }

Use the InvariantCulture so that, whatever the regional configuration, the . is expected as a decimal separator.

Greetings

    
answered by 13.06.2017 в 17:05
1

Assuming you already have the event onKeyDown done (otherwise it should be the first thing you do), you have to check that the key you press is the Enter:

    private void nombreCampo_Keydown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
    {


      double venta= double.Parse(txtcompra.Text) * (double.Parse(txtganancia.Text)/100)+ double.Parse(txtcompra.Text);

    txtventa.Text=venta.ToString();
    }

}

I've done it with double , but you can do with float perfectly.

To avoid the conversion error that appears to you, you can use the float.TryParse function, which means that if the value you have inserted can not be formatted, it does not format it:

double compra=0;
double ganancia=0;
double.TryParse(txtcompra.Text, out compra);
double.TryParse(txtganancia.Text, out ganancia);
double venta= compra * (ganancia/100)+ compra;
    
answered by 13.06.2017 в 16:35
-1

It would be something like:

        float pventa, pcompra, ganancia;
        pcompra = float.Parse(txtcompra.Text);
        ganancia = float.Parse(txtganancia.Text);

        pventa = pcompra + pcompra * ganancia / 100;
        txtventa.Text = pventa.ToString();

It would be necessary to add the validations from the parse to the float. That remains of homework.

    
answered by 13.06.2017 в 16:22