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. round off problem

round off problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
6 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.
  • W Offline
    W Offline
    wong1907
    wrote on last edited by
    #1

    Hello, How to round a number into nearest integer in C++? Is there any function to do that? THX!!! :):):)

    R P D 3 Replies Last reply
    0
    • W wong1907

      Hello, How to round a number into nearest integer in C++? Is there any function to do that? THX!!! :):):)

      R Offline
      R Offline
      Rickard Andersson20
      wrote on last edited by
      #2

      double a_bouble_val = 23,34;
      int an_integer = (int)a_double_val;

      This small code will assign an_integer to 23.

      double a_bouble_val = 23,7;
      int an_integer = (int)a_double_val;

      This, will assign an_integer to 24. Did I understand you? Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!

      P 1 Reply Last reply
      0
      • W wong1907

        Hello, How to round a number into nearest integer in C++? Is there any function to do that? THX!!! :):):)

        P Offline
        P Offline
        PJ Arends
        wrote on last edited by
        #3

        You can use floor(), ceil(), or just cast to an integer. This code rounds a value to the nearest integer.

        int rounded = (int)(doubleval + 0.5);


        CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!

        C 1 Reply Last reply
        0
        • R Rickard Andersson20

          double a_bouble_val = 23,34;
          int an_integer = (int)a_double_val;

          This small code will assign an_integer to 23.

          double a_bouble_val = 23,7;
          int an_integer = (int)a_double_val;

          This, will assign an_integer to 24. Did I understand you? Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!

          P Offline
          P Offline
          PJ Arends
          wrote on last edited by
          #4

          Rickard Andersson wrote: double a_bouble_val = 23,7;int an_integer = (int)a_double_val; This, will assign an_integer to 24. Actually, this will evaluate to 23. Casting a double to an int will always round to the whole number given (down for positive numbers, up for negative)


          CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!

          1 Reply Last reply
          0
          • W wong1907

            Hello, How to round a number into nearest integer in C++? Is there any function to do that? THX!!! :):):)

            D Offline
            D Offline
            David Chamberlain
            wrote on last edited by
            #5

            It's a little more complicated if you are going to be dealing with both positive and negative numbers, as the floor() function moves the decimal number toward zero and the ceil() function moves the decimal number away from zero. The value of floor(-23.7) is -23, where rounded you probably would want -24. Adding 0.5 to the value before casting to an int works only for positive values, and not for negatives. There, you would have to subtract. floor(-23.7 - 0.5) = floor(-24.2) = 24 floor( 23.7 + 0.5) = floor( 24.2) = 24 Dave "You can say that again." -- Dept. of Redundancy Dept.

            1 Reply Last reply
            0
            • P PJ Arends

              You can use floor(), ceil(), or just cast to an integer. This code rounds a value to the nearest integer.

              int rounded = (int)(doubleval + 0.5);


              CPUA 0x5041 Sonork 100.11743 Chicken Little "So it can now be written in stone as a testament to humanities achievments "PJ did Pi at CP"." Colin Davies Within you lies the power for good - Use it!

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              it rounds up, to the nearest integer. this goes both ways. int i = (d > 0 ? (int)(d + 0.5) : (int)(d - 0.5)); -c


              WWT2D?

              Fractals

              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