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 / C++ / MFC
  4. Convert from Double to Integer w/o Rounding in C++

Convert from Double to Integer w/o Rounding in C++

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
8 Posts 5 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 Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi, I want to be able to convert a double to an integer. Say the double variable is a decimal, like 1.732. If I use int() to convert it, I will get 2. I just want the whole number of the double variable, 1, without any rounding. How do I do this in C++?

    L _ B A 4 Replies Last reply
    0
    • L Lost User

      Hi, I want to be able to convert a double to an integer. Say the double variable is a decimal, like 1.732. If I use int() to convert it, I will get 2. I just want the whole number of the double variable, 1, without any rounding. How do I do this in C++?

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

      I would try a simple cast. Warning: whatever you end up with, make sure it works the way you want it for negative numbers too! :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      L 1 Reply Last reply
      0
      • L Luc Pattyn

        I would try a simple cast. Warning: whatever you end up with, make sure it works the way you want it for negative numbers too! :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

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

        Don't typecasts round the decimal to a whole number when going from double to integer? And isn't int() a typecast? BTW thanks for the help...

        L 1 Reply Last reply
        0
        • L Lost User

          Don't typecasts round the decimal to a whole number when going from double to integer? And isn't int() a typecast? BTW thanks for the help...

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

          did you try anything? did you look it up in your C++ book? did you google it? see, asking questions is the easy part. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          1 Reply Last reply
          0
          • L Lost User

            Hi, I want to be able to convert a double to an integer. Say the double variable is a decimal, like 1.732. If I use int() to convert it, I will get 2. I just want the whole number of the double variable, 1, without any rounding. How do I do this in C++?

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            Casting and assigning to an integer will not do any rounding. The integer will only store what it can accommodate, which is the integer part of the decimal number.

            «_Superman_»  _I love work. It gives me something to do between weekends.

            _Microsoft MVP (Visual C++)

            Polymorphism in C

            1 Reply Last reply
            0
            • L Lost User

              Hi, I want to be able to convert a double to an integer. Say the double variable is a decimal, like 1.732. If I use int() to convert it, I will get 2. I just want the whole number of the double variable, 1, without any rounding. How do I do this in C++?

              B Offline
              B Offline
              Bernhard Hiller
              wrote on last edited by
              #6

              What about using floor or ceil before casting to int? Actually, the return value of those function is double again, but I do not know how bad the consequences could be - maybe round after those functions...

              1 Reply Last reply
              0
              • L Lost User

                Hi, I want to be able to convert a double to an integer. Say the double variable is a decimal, like 1.732. If I use int() to convert it, I will get 2. I just want the whole number of the double variable, 1, without any rounding. How do I do this in C++?

                A Offline
                A Offline
                Andrew Brock
                wrote on last edited by
                #7

                I thought it was a standard thing, but when I do int(1.732) I get 1 and int(-1.732) I get -1. A look at the disassembly shows that a function from the CRT (_ftol2 in my case) is used, so it could be implementation specific. If this is the case then you should use something like floor/ceil as Bernhard Hiller suggested. This function will result in the behaviour described above, where numbers round towards 0. It includes epsilon rounding to account for epsilon errors. In some cases 3 = 2.99999999999 (or something similar) due to rounding errors in previous operations. See http://en.wikipedia.org/wiki/Double_precision[^] for more details

                int DoubleToInt(double nDouble) {
                if (nDouble >= 0) {
                retrun (int)floor(nDouble + DBL_EPSILON);
                } else {
                retrun (int)ceil(nDouble - DBL_EPSILON);
                }
                }

                L 1 Reply Last reply
                0
                • A Andrew Brock

                  I thought it was a standard thing, but when I do int(1.732) I get 1 and int(-1.732) I get -1. A look at the disassembly shows that a function from the CRT (_ftol2 in my case) is used, so it could be implementation specific. If this is the case then you should use something like floor/ceil as Bernhard Hiller suggested. This function will result in the behaviour described above, where numbers round towards 0. It includes epsilon rounding to account for epsilon errors. In some cases 3 = 2.99999999999 (or something similar) due to rounding errors in previous operations. See http://en.wikipedia.org/wiki/Double_precision[^] for more details

                  int DoubleToInt(double nDouble) {
                  if (nDouble >= 0) {
                  retrun (int)floor(nDouble + DBL_EPSILON);
                  } else {
                  retrun (int)ceil(nDouble - DBL_EPSILON);
                  }
                  }

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

                  Thanks. I thought I read somewhere that int() rounded down when converting from double, though it didn't seem so when I tested it. I think the whole thing that brought up this issue was the fact that int() doesn't handle decimals like 1.99999... correctly. Therefore it was throwing off everything when it rounded 1.999... to 2. The above function works perfectly, so long as you make sure to include both float.h and cmath. Thanks again!

                  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