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 doubles to set # decimal places

rounding doubles to set # decimal places

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
6 Posts 3 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.
  • B Offline
    B Offline
    b rad311
    wrote on last edited by
    #1

    Hi, Does anyone know if there's a built in function in c++ or mfc in which you can pass a double or long double and declare how many places you'd like the value to be rounded to? For example, pass it 12.125 and n=2 decimal places, and out comes 12.13, etc. Thanks! P.S. Formatting the values during cout won't work for me, I need to store the rounded values in a new variable.

    A CPalliniC 2 Replies Last reply
    0
    • B b rad311

      Hi, Does anyone know if there's a built in function in c++ or mfc in which you can pass a double or long double and declare how many places you'd like the value to be rounded to? For example, pass it 12.125 and n=2 decimal places, and out comes 12.13, etc. Thanks! P.S. Formatting the values during cout won't work for me, I need to store the rounded values in a new variable.

      A Offline
      A Offline
      Aescleal
      wrote on last edited by
      #2

      I don't do a lot of numeric stuff anymore so take this with a pinch of salt. First off I don't know any C++ standard library facilities that would help. C had floor and ceil which take doubles and return an integer either truncating the value or rounding up to the next highest number. With these and pow you can write something that'll be good enough for most things. Off the top of my head it'd look like:

      double round_to( double number, unsigned number_of_decimal_places )
      {
      double exponent( pow( 10, number_of_decimal_places ) );
      number *= exponent; // disaster line!
      number += 0.5;
      return floor( number ) / exponent;
      }

      which is a bit cheesy - it won't work for negative numbers or any number that causes the line with the "disaster line!" comment to overflow an integer. However they might give you a handle if no one comes up with a better idea! Cheers, Ash Edited to remove a spurious brace...

      modified on Friday, June 18, 2010 3:52 PM

      B 1 Reply Last reply
      0
      • B b rad311

        Hi, Does anyone know if there's a built in function in c++ or mfc in which you can pass a double or long double and declare how many places you'd like the value to be rounded to? For example, pass it 12.125 and n=2 decimal places, and out comes 12.13, etc. Thanks! P.S. Formatting the values during cout won't work for me, I need to store the rounded values in a new variable.

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

        You may use the dirty old town's trick:

        double rounded(double x, int n)
        {
        double d = pow(10, (double)n);
        return ((int)(x * d + .5))/d;
        }

        :)

        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.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        B 1 Reply Last reply
        0
        • CPalliniC CPallini

          You may use the dirty old town's trick:

          double rounded(double x, int n)
          {
          double d = pow(10, (double)n);
          return ((int)(x * d + .5))/d;
          }

          :)

          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.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          B Offline
          B Offline
          b rad311
          wrote on last edited by
          #4

          Excellent, this did exactly what I was needing. Thanks! One thing I noticed was when I used -265.3215894 with n=2, it returns -265.31 instead of -265.32 Any thoughts on this?

          CPalliniC 1 Reply Last reply
          0
          • A Aescleal

            I don't do a lot of numeric stuff anymore so take this with a pinch of salt. First off I don't know any C++ standard library facilities that would help. C had floor and ceil which take doubles and return an integer either truncating the value or rounding up to the next highest number. With these and pow you can write something that'll be good enough for most things. Off the top of my head it'd look like:

            double round_to( double number, unsigned number_of_decimal_places )
            {
            double exponent( pow( 10, number_of_decimal_places ) );
            number *= exponent; // disaster line!
            number += 0.5;
            return floor( number ) / exponent;
            }

            which is a bit cheesy - it won't work for negative numbers or any number that causes the line with the "disaster line!" comment to overflow an integer. However they might give you a handle if no one comes up with a better idea! Cheers, Ash Edited to remove a spurious brace...

            modified on Friday, June 18, 2010 3:52 PM

            B Offline
            B Offline
            b rad311
            wrote on last edited by
            #5

            Thanks for the help Ash!

            1 Reply Last reply
            0
            • B b rad311

              Excellent, this did exactly what I was needing. Thanks! One thing I noticed was when I used -265.3215894 with n=2, it returns -265.31 instead of -265.32 Any thoughts on this?

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

              As Aescleal stated, that doesn't work for negative numbers. However you may test for the sign and act accordingly (subtracting .5, instead of adding it). :)

              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.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              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