Result of division not appear in the textbox
-
If I put the values 5 and 2 in the TextBoxes txtValor1 and txtValor2, in the TextBox txtResultado appear 2 and not 2.5 . Why? My code is:
long valor1,valor2; double resultado; valor1 = int.Parse(txtValor1.Text); valor2 = int.Parse(txtValor2.Text); if (opcDivide.Checked) { resultado=valor1/valor2; txtResultado.Text=Convert.ToString(resultado); }
Thanks. -
If I put the values 5 and 2 in the TextBoxes txtValor1 and txtValor2, in the TextBox txtResultado appear 2 and not 2.5 . Why? My code is:
long valor1,valor2; double resultado; valor1 = int.Parse(txtValor1.Text); valor2 = int.Parse(txtValor2.Text); if (opcDivide.Checked) { resultado=valor1/valor2; txtResultado.Text=Convert.ToString(resultado); }
Thanks. -
If I put the values 5 and 2 in the TextBoxes txtValor1 and txtValor2, in the TextBox txtResultado appear 2 and not 2.5 . Why? My code is:
long valor1,valor2; double resultado; valor1 = int.Parse(txtValor1.Text); valor2 = int.Parse(txtValor2.Text); if (opcDivide.Checked) { resultado=valor1/valor2; txtResultado.Text=Convert.ToString(resultado); }
Thanks.What did the debugger indicate?
Vasudevan Deepak Kumar Personal Homepage
Tech Gossips
A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson -
If I put the values 5 and 2 in the TextBoxes txtValor1 and txtValor2, in the TextBox txtResultado appear 2 and not 2.5 . Why? My code is:
long valor1,valor2; double resultado; valor1 = int.Parse(txtValor1.Text); valor2 = int.Parse(txtValor2.Text); if (opcDivide.Checked) { resultado=valor1/valor2; txtResultado.Text=Convert.ToString(resultado); }
Thanks.PeterRoman wrote:
If I put the values 5 and 2 in the TextBoxes txtValor1 and txtValor2, in the TextBox txtResultado appear 2 and not 2.5 . Why?
Because the result of an integer division is an int. Thus the correct answer for 5/2 is 2. If you want an answer of 2.5 you must use doubles.
Upcoming FREE developer events: * Developer Day Scotland My website
-
If I put the values 5 and 2 in the TextBoxes txtValor1 and txtValor2, in the TextBox txtResultado appear 2 and not 2.5 . Why? My code is:
long valor1,valor2; double resultado; valor1 = int.Parse(txtValor1.Text); valor2 = int.Parse(txtValor2.Text); if (opcDivide.Checked) { resultado=valor1/valor2; txtResultado.Text=Convert.ToString(resultado); }
Thanks.double valor1,valor2; double resultado; valor1 = (double) txtValor1.Text; valor2 = (double) txtValor2.Text; if (opcDivide.Checked) { resultado=valor1/valor2; txtResultado.Text=Convert.ToString(resultado); }
You code will fall over as soon as some bright spark enters a non number into your texbox, put a validator on it.Grady Booch: I told Google to their face...what you need is some serious adult supervision. (2007 Turing lecture) http://www.frankkerrigan.com/[^]
-
THe Result has to be cast into string... TxtResult.Text= result.ToString(); -Sreejith
-
double valor1,valor2; double resultado; valor1 = (double) txtValor1.Text; valor2 = (double) txtValor2.Text; if (opcDivide.Checked) { resultado=valor1/valor2; txtResultado.Text=Convert.ToString(resultado); }
You code will fall over as soon as some bright spark enters a non number into your texbox, put a validator on it.Grady Booch: I told Google to their face...what you need is some serious adult supervision. (2007 Turing lecture) http://www.frankkerrigan.com/[^]
Though this isn't particularly robust code. Better would be to use something like:
double valor1, valor2; double resultado; if (!double.TryParse(txtValor1.Text, valor1)) { valor1 = 0; } if (!double.TryParse(txtValor2.Text, valor2)) { valor2 = 0; } if (opcDivide.Checked) { try { resultado = valor1/valor2; txtResultado.Text = resultado.ToString(); } catch (DivideByZeroException e) { txtResultado.Text = "Error"; } }
HTH DaveIt definitely isn't definatley
-
Though this isn't particularly robust code. Better would be to use something like:
double valor1, valor2; double resultado; if (!double.TryParse(txtValor1.Text, valor1)) { valor1 = 0; } if (!double.TryParse(txtValor2.Text, valor2)) { valor2 = 0; } if (opcDivide.Checked) { try { resultado = valor1/valor2; txtResultado.Text = resultado.ToString(); } catch (DivideByZeroException e) { txtResultado.Text = "Error"; } }
HTH DaveIt definitely isn't definatley
Hi, two comments: 1.
moon_stick wrote:
if (!double.TryParse(txtValor1.Text, valor1)) { valor1 = 0; }
can be simplified to
double.TryParse(txtValor1.Text, valor1)
which does exactly the same, given the following from MSDN: "When this method returns, contains the double-precision floating-point number equivalent to the s parameter, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not a number in a valid format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized." 2. It would be better to produce some error indication when TryParse fails. With your code: - a mistake in valor1 results in zero, which may be confusing - a mistake in valor2 results in a divide by zero with a misleading error text :)Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets
-
Though this isn't particularly robust code. Better would be to use something like:
double valor1, valor2; double resultado; if (!double.TryParse(txtValor1.Text, valor1)) { valor1 = 0; } if (!double.TryParse(txtValor2.Text, valor2)) { valor2 = 0; } if (opcDivide.Checked) { try { resultado = valor1/valor2; txtResultado.Text = resultado.ToString(); } catch (DivideByZeroException e) { txtResultado.Text = "Error"; } }
HTH DaveIt definitely isn't definatley
This code is very good and the Dave too. :) Thanks.
-
Hi, two comments: 1.
moon_stick wrote:
if (!double.TryParse(txtValor1.Text, valor1)) { valor1 = 0; }
can be simplified to
double.TryParse(txtValor1.Text, valor1)
which does exactly the same, given the following from MSDN: "When this method returns, contains the double-precision floating-point number equivalent to the s parameter, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is a null reference (Nothing in Visual Basic), is not a number in a valid format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized." 2. It would be better to produce some error indication when TryParse fails. With your code: - a mistake in valor1 results in zero, which may be confusing - a mistake in valor2 results in a divide by zero with a misleading error text :)Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets
Hi Luc, Of course you're right - I just couldn't be bothered checking the MSDN documentation to see what was returned if the cast failed sp decided to try and cover my bases. I was treating the code as if it were working on a simple calculator and limited the range of error messages. In any case, I said it was better, not perfect!! :laugh: Dave
It definitely isn't definatley