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. about "round" function in visual C++

about "round" function in visual C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
5 Posts 4 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.
  • M Offline
    M Offline
    mt_samiei
    wrote on last edited by
    #1

    Dear all I have one double number which i want to round to nearest decimal number in Visual C++. What is the function to do this REgards Monhi

    CPalliniC 1 Reply Last reply
    0
    • M mt_samiei

      Dear all I have one double number which i want to round to nearest decimal number in Visual C++. What is the function to do this REgards Monhi

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      double d =whatever;
      int i = (int)(d + .5);

      :) Please note that, as pointed out by Rage it doesn't work properly with negative numbers See the fix here [^]. -- modified at 8:10 Tuesday 10th July, 2007

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      In testa che avete, signor di Ceprano?

      R 1 Reply Last reply
      0
      • CPalliniC CPallini

        double d =whatever;
        int i = (int)(d + .5);

        :) Please note that, as pointed out by Rage it doesn't work properly with negative numbers See the fix here [^]. -- modified at 8:10 Tuesday 10th July, 2007

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        R Offline
        R Offline
        Rage
        wrote on last edited by
        #3

        d=-5.6 => i=-5, not -6. Is this ok ?

        http://www.readytogiveup.com/[^] - Do something special today.

        T CPalliniC 2 Replies Last reply
        0
        • R Rage

          d=-5.6 => i=-5, not -6. Is this ok ?

          http://www.readytogiveup.com/[^] - Do something special today.

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          he probably should take the absolute value, and then apply it the original sign...


          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          1 Reply Last reply
          0
          • R Rage

            d=-5.6 => i=-5, not -6. Is this ok ?

            http://www.readytogiveup.com/[^] - Do something special today.

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Rage wrote:

            d=-5.6 => i=-5, not -6. Is this ok ?

            No, of course. I (naively) assumed d > 0. But there is an easy fix to account for negative numbers:

            i = (int) ( d < .0 ? d -.5 : d + .5 );

            :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            In testa che avete, signor di Ceprano?

            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