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. Noob question, probably easy answer...

Noob question, probably easy answer...

Scheduled Pinned Locked Moved C#
questioncsharphelplearning
18 Posts 6 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.
  • L Lost User

    Lodeclaw wrote:

    int tc = 5 / 9 * (tf - 32);

    Integer devision, a common mistake for beginners, it happens to all of us. 5/9 is .5555 which is 0. Convert to doubles and try it then, should be fine after that.

    L Offline
    L Offline
    Lodeclaw
    wrote on last edited by
    #3

    It's the same result.

    L K 2 Replies Last reply
    0
    • L Lodeclaw

      It's the same result.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      No it isn't... I used same variable names...

              double tf = double.Parse(textBox1.Text);
      
              double tc = (5.0 / 9.0) \* (tf - 32);
              label1.Text = tc.ToString();
      
      L 1 Reply Last reply
      0
      • L Lodeclaw

        It's the same result.

        K Offline
        K Offline
        Kristian Sixhoj
        wrote on last edited by
        #5

        double tf = double.Parse(textBox1.Text);

        double tc = 5 / 9 * (tf - 32);
        label1.Text = tc.ToString();

        If that doesn't work, something's really wrong here. :rolleyes:

        Kristian Sixhoej "You can't undo the past... but you can certainly not repeat it." - Bruce Willis

        L 1 Reply Last reply
        0
        • K Kristian Sixhoj

          double tf = double.Parse(textBox1.Text);

          double tc = 5 / 9 * (tf - 32);
          label1.Text = tc.ToString();

          If that doesn't work, something's really wrong here. :rolleyes:

          Kristian Sixhoej "You can't undo the past... but you can certainly not repeat it." - Bruce Willis

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          Kristian Sixhoej wrote:

          If that doesn't work, something's really wrong here.

          Untrue, I think it will still do integer devision on 5 / 9, you have to explicitly put .0 on either to do double devision. Weird, I know, I just noticed that behavior on a test.

          K 1 Reply Last reply
          0
          • L Lost User

            No it isn't... I used same variable names...

                    double tf = double.Parse(textBox1.Text);
            
                    double tc = (5.0 / 9.0) \* (tf - 32);
                    label1.Text = tc.ToString();
            
            L Offline
            L Offline
            Lodeclaw
            wrote on last edited by
            #7

            Oh, I see the problem. We're not entering the values in the calculation as doubles. (5.0 rather than 5) Thanks, Eliott.

            L 2 Replies Last reply
            0
            • L Lodeclaw

              Oh, I see the problem. We're not entering the values in the calculation as doubles. (5.0 rather than 5) Thanks, Eliott.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              Anytime

              1 Reply Last reply
              0
              • L Lost User

                Kristian Sixhoej wrote:

                If that doesn't work, something's really wrong here.

                Untrue, I think it will still do integer devision on 5 / 9, you have to explicitly put .0 on either to do double devision. Weird, I know, I just noticed that behavior on a test.

                K Offline
                K Offline
                Kristian Sixhoj
                wrote on last edited by
                #9

                EliottA wrote:

                you have to explicitly put .0 on either to do double devision.

                Didn't knew that.

                EliottA wrote:

                Weird, I know, I just noticed that behavior on a test.

                IIRC I have never had that problem when doing double divisions. But I probably don't recall correct. ;P

                Kristian Sixhoej "You can't undo the past... but you can certainly not repeat it." - Bruce Willis

                L R 2 Replies Last reply
                0
                • L Lodeclaw

                  Oh, I see the problem. We're not entering the values in the calculation as doubles. (5.0 rather than 5) Thanks, Eliott.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  Remember to change your int.Parse and datatype to double and double.Parse otherwise entering 10.0 in that textbox and parsing it to an int might either through an exception or truncate the double value (I don't know which one) in either case, duplicating the original error!!

                  1 Reply Last reply
                  0
                  • K Kristian Sixhoj

                    EliottA wrote:

                    you have to explicitly put .0 on either to do double devision.

                    Didn't knew that.

                    EliottA wrote:

                    Weird, I know, I just noticed that behavior on a test.

                    IIRC I have never had that problem when doing double divisions. But I probably don't recall correct. ;P

                    Kristian Sixhoej "You can't undo the past... but you can certainly not repeat it." - Bruce Willis

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #11

                    I didn't know it either, and I never ran into a problem like that before, I never had to implicitly add a .0 unless it was in java.. even then I vaguely recall something about automatic upgrading datatype or something, I don't know. I completely forget, just remember seeing integer devision smack me in the face a few times in my first programming course.

                    G 1 Reply Last reply
                    0
                    • K Kristian Sixhoj

                      EliottA wrote:

                      you have to explicitly put .0 on either to do double devision.

                      Didn't knew that.

                      EliottA wrote:

                      Weird, I know, I just noticed that behavior on a test.

                      IIRC I have never had that problem when doing double divisions. But I probably don't recall correct. ;P

                      Kristian Sixhoej "You can't undo the past... but you can certainly not repeat it." - Bruce Willis

                      R Offline
                      R Offline
                      Rob Philpott
                      wrote on last edited by
                      #12

                      Being a bit anal about this, I'd do this:

                      double tf = double.Parse(textBox1.Text);
                      double tc = 5d / 9d * (tf - 32d);
                      label1.Text = tc.ToString();

                      Regards, Rob Philpott.

                      L 1 Reply Last reply
                      0
                      • R Rob Philpott

                        Being a bit anal about this, I'd do this:

                        double tf = double.Parse(textBox1.Text);
                        double tc = 5d / 9d * (tf - 32d);
                        label1.Text = tc.ToString();

                        Regards, Rob Philpott.

                        L Offline
                        L Offline
                        Lodeclaw
                        wrote on last edited by
                        #13

                        Oh, that's fascinating! Placing a 'd' after a whole number will really make it a double?

                        L 1 Reply Last reply
                        0
                        • L Lodeclaw

                          Oh, that's fascinating! Placing a 'd' after a whole number will really make it a double?

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #14

                          Yep, although I never really have ever used it. I'd rather put 5.0 or something of the sort.

                          M 1 Reply Last reply
                          0
                          • L Lost User

                            Yep, although I never really have ever used it. I'd rather put 5.0 or something of the sort.

                            M Offline
                            M Offline
                            musefan
                            wrote on last edited by
                            #15

                            will 5.0 def making it a double as opposed to float? Im not trying to correct, seroius question

                            My opinion is... If someone has already posted an answer, dont post the SAME answer

                            L 1 Reply Last reply
                            0
                            • M musefan

                              will 5.0 def making it a double as opposed to float? Im not trying to correct, seroius question

                              My opinion is... If someone has already posted an answer, dont post the SAME answer

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #16

                              From my recollection yes it will, the default is always double unless explicitly indicated otherwise.

                              M 1 Reply Last reply
                              0
                              • L Lost User

                                From my recollection yes it will, the default is always double unless explicitly indicated otherwise.

                                M Offline
                                M Offline
                                musefan
                                wrote on last edited by
                                #17

                                ;)

                                My opinion is... If someone has already posted an answer, dont post the SAME answer

                                1 Reply Last reply
                                0
                                • L Lost User

                                  I didn't know it either, and I never ran into a problem like that before, I never had to implicitly add a .0 unless it was in java.. even then I vaguely recall something about automatic upgrading datatype or something, I don't know. I completely forget, just remember seeing integer devision smack me in the face a few times in my first programming course.

                                  G Offline
                                  G Offline
                                  Guffa
                                  wrote on last edited by
                                  #18

                                  EliottA wrote:

                                  I never ran into a problem like that before

                                  That's probably because it's not that usual to divide two literal values. Usually one of the operands is a double variable, then the compiler will cast the other operand to double also.

                                  Despite everything, the person most likely to be fooling you next is yourself.

                                  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