Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Result of division not appear in the textbox

Result of division not appear in the textbox

Scheduled Pinned Locked Moved C#
question
10 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PeterRoman
    wrote on last edited by
    #1

    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.

    S V C F 4 Replies Last reply
    0
    • P PeterRoman

      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.

      S Offline
      S Offline
      Shpendh
      wrote on last edited by
      #2

      when you divide this two values try to cast and initialize to result

      spaps

      S 1 Reply Last reply
      0
      • P PeterRoman

        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.

        V Offline
        V Offline
        Vasudevan Deepak Kumar
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • P PeterRoman

          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.

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • P PeterRoman

            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.

            F Offline
            F Offline
            Frank Kerrigan
            wrote on last edited by
            #5

            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/[^]

            M 1 Reply Last reply
            0
            • S Shpendh

              when you divide this two values try to cast and initialize to result

              spaps

              S Offline
              S Offline
              sreejithnbr
              wrote on last edited by
              #6

              THe Result has to be cast into string... TxtResult.Text= result.ToString(); -Sreejith

              1 Reply Last reply
              0
              • F Frank Kerrigan

                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/[^]

                M Offline
                M Offline
                moon_stick
                wrote on last edited by
                #7

                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 Dave

                It definitely isn't definatley

                L P 2 Replies Last reply
                0
                • M moon_stick

                  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 Dave

                  It definitely isn't definatley

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  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


                  M 1 Reply Last reply
                  0
                  • M moon_stick

                    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 Dave

                    It definitely isn't definatley

                    P Offline
                    P Offline
                    PeterRoman
                    wrote on last edited by
                    #9

                    This code is very good and the Dave too. :) Thanks.

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      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


                      M Offline
                      M Offline
                      moon_stick
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups