1

I want to to multiply textbox1(int) by textbox2(double) and return a double in textbox3 Im doing an Invoice app and need to multiply quantity by rate to get amount so far I have this...

private void textbox2_TextChanged(object sender, system.EventArgs e)
{
 int32 qty = int32.Parse(Textbox1.text);
 Double rate = Double.Parse(Textbox2.text);
 Double amt = qty * rate;
 textbox3.text = amt.ToString();
}

Where am I going wrong?

ghostJago
  • 3,381
  • 5
  • 36
  • 51
Jose Rivas
  • 59
  • 1
  • 1
  • 9

3 Answers3

4

First of all you need to check whether the first text box is not empty or blank spaces. Then if your first text box is empty or blank space then give appropriate message to user.

Try the example below:

if (!string.IsNullOrWhiteSpace(Textbox1.text))
{
    int qty = Int32.Parse(Textbox1.text);
    double rate = Double.Parse(Textbox2.text);
    Textbox3.text = (rate* (double) qty).ToString();
}
else
{
    //Give appropriate message to user for entering quantity in textbox 1
}
ivcubr
  • 1,988
  • 9
  • 20
  • 28
Tejas Vaishnav
  • 466
  • 5
  • 20
1

Try this out

  textBox3.Text = (Int32.Parse(textBox1.Text) * Double.Parse(textBox2.Text)).ToString();
Nejthe
  • 353
  • 1
  • 4
  • 17
0

First off. When you are dealing with money, always use decimal, not double. And with decimals, there is always the problem with users using both comma and dot as decimal separator.

This code worked for me:

    int qty = Int32.Parse("2");
    Decimal rate = Decimal.Parse("3.14".Replace(".", ","));
    Decimal amt = qty * rate;
    Console.WriteLine(amt);