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. Rounding float values

Rounding float values

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
10 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.
  • K Offline
    K Offline
    KASR1
    wrote on last edited by
    #1

    Is there any function to do the following operation in CPP? Round(3.4) = 3 Round(3.5) = 4

    N Steve EcholsS D 3 Replies Last reply
    0
    • K KASR1

      Is there any function to do the following operation in CPP? Round(3.4) = 3 Round(3.5) = 4

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      ceil() function? ceil( 3.4 + 0.5 ) = 3; ceil( 3.5 + 0.5 ) = 4;

      nave [OpenedFileFinder] [My Blog]

      K 1 Reply Last reply
      0
      • N Naveen

        ceil() function? ceil( 3.4 + 0.5 ) = 3; ceil( 3.5 + 0.5 ) = 4;

        nave [OpenedFileFinder] [My Blog]

        K Offline
        K Offline
        KASR1
        wrote on last edited by
        #3

        see ceil(3.4) will return 4. please check.

        M C N 3 Replies Last reply
        0
        • K KASR1

          see ceil(3.4) will return 4. please check.

          M Offline
          M Offline
          Malli_S
          wrote on last edited by
          #4

          did u try any of

          double floor(double x);
          float floor(float x); // C++ only
          long double floor(long double x); // C++ only
          float floorf(float x);

          -Malli...! :rose:****

          1 Reply Last reply
          0
          • K KASR1

            Is there any function to do the following operation in CPP? Round(3.4) = 3 Round(3.5) = 4

            Steve EcholsS Offline
            Steve EcholsS Offline
            Steve Echols
            wrote on last edited by
            #5

            Try: floor(x + 0.5)


            - S 50 cups of coffee and you know it's on! A post a day, keeps the white coats away!

            • S
              50 cups of coffee and you know it's on!
              Code, follow, or get out of the way.
            1 Reply Last reply
            0
            • K KASR1

              see ceil(3.4) will return 4. please check.

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              You have to use floor() and add 0.5 to the number you want to round.

              Cédric Moonen Software developer
              Charting control [v1.5] OpenGL game tutorial in C++

              K 1 Reply Last reply
              0
              • K KASR1

                see ceil(3.4) will return 4. please check.

                N Offline
                N Offline
                Naveen
                wrote on last edited by
                #7

                :doh: ..am sorry.. I was ment to say floor() function. Please you floor().

                nave [OpenedFileFinder] [My Blog]

                K 1 Reply Last reply
                0
                • C Cedric Moonen

                  You have to use floor() and add 0.5 to the number you want to round.

                  Cédric Moonen Software developer
                  Charting control [v1.5] OpenGL game tutorial in C++

                  K Offline
                  K Offline
                  KASR1
                  wrote on last edited by
                  #8

                  Working now.

                  1 Reply Last reply
                  0
                  • N Naveen

                    :doh: ..am sorry.. I was ment to say floor() function. Please you floor().

                    nave [OpenedFileFinder] [My Blog]

                    K Offline
                    K Offline
                    KASR1
                    wrote on last edited by
                    #9

                    Yes its working now. Thanks.

                    1 Reply Last reply
                    0
                    • K KASR1

                      Is there any function to do the following operation in CPP? Round(3.4) = 3 Round(3.5) = 4

                      D Offline
                      D Offline
                      Dan 0
                      wrote on last edited by
                      #10

                      Fastest way, assembler automatically will do rounding for you, but std lib does it some bakwards way

                      inline int iround(double v)
                      {
                      int retval;
                      __asm fld qword ptr [v]
                      __asm fistp dword ptr [retval]
                      return retval;
                      }

                      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