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 double to a provided decimal place

Rounding double to a provided decimal place

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

    Hi All. A very simple question, but so far not able to find any concrete solution. I just want to round a decimal value to a provided decimal place. I have found few functions to do the same, but they also fail for some double values...like...

    double Round1(double dbVal, int nPlaces)
    {
    const double dbShift = pow(10.0, nPlaces);
    return floor(dbVal * dbShift + 0.5) / dbShift;
    }

    double Round2(double value,int pos)
    {
    double returnValue;
    double tens = exp(pos*log(10.0));
    if((value - floor(value*tens)/tens)*tens >= 0.5)
    returnValue = ceil(value*tens)/tens
    else
    returnValue = floor(value*tens)/tens;

    return returnValue;
    

    }

    Both of above function fails in case the double value is 10430.889999999999 464.45999999999998 3294.5100000000002 Please suggest. Thanks PanB

    T _ L C 5 Replies Last reply
    0
    • P PankajB

      Hi All. A very simple question, but so far not able to find any concrete solution. I just want to round a decimal value to a provided decimal place. I have found few functions to do the same, but they also fail for some double values...like...

      double Round1(double dbVal, int nPlaces)
      {
      const double dbShift = pow(10.0, nPlaces);
      return floor(dbVal * dbShift + 0.5) / dbShift;
      }

      double Round2(double value,int pos)
      {
      double returnValue;
      double tens = exp(pos*log(10.0));
      if((value - floor(value*tens)/tens)*tens >= 0.5)
      returnValue = ceil(value*tens)/tens
      else
      returnValue = floor(value*tens)/tens;

      return returnValue;
      

      }

      Both of above function fails in case the double value is 10430.889999999999 464.45999999999998 3294.5100000000002 Please suggest. Thanks PanB

      T Offline
      T Offline
      T2102
      wrote on last edited by
      #2

      I haven't looks at Round2, but cannot imagine a case where you would need to use exp and log to round.

      1 Reply Last reply
      0
      • P PankajB

        Hi All. A very simple question, but so far not able to find any concrete solution. I just want to round a decimal value to a provided decimal place. I have found few functions to do the same, but they also fail for some double values...like...

        double Round1(double dbVal, int nPlaces)
        {
        const double dbShift = pow(10.0, nPlaces);
        return floor(dbVal * dbShift + 0.5) / dbShift;
        }

        double Round2(double value,int pos)
        {
        double returnValue;
        double tens = exp(pos*log(10.0));
        if((value - floor(value*tens)/tens)*tens >= 0.5)
        returnValue = ceil(value*tens)/tens
        else
        returnValue = floor(value*tens)/tens;

        return returnValue;
        

        }

        Both of above function fails in case the double value is 10430.889999999999 464.45999999999998 3294.5100000000002 Please suggest. Thanks PanB

        _ Offline
        _ Offline
        _AnsHUMAN_
        wrote on last edited by
        #3

        Is it a possibility for you to convert this to CString (if using MFC) or char* (using sprintf_s)? If affirmative, use the format specifiers and call API CString::Format or sprintf_s and then convert back to double value when returning from your function.

        I am a HUMAN. I have that keyword in my name........ ;-)_AnsHUMAN_

        P C 2 Replies Last reply
        0
        • _ _AnsHUMAN_

          Is it a possibility for you to convert this to CString (if using MFC) or char* (using sprintf_s)? If affirmative, use the format specifiers and call API CString::Format or sprintf_s and then convert back to double value when returning from your function.

          I am a HUMAN. I have that keyword in my name........ ;-)_AnsHUMAN_

          P Offline
          P Offline
          PankajB
          wrote on last edited by
          #4

          Converting double to CString can be done, but to round off the same will take lots of permutations and combinations because we dont know upto what decimal place we need rounding. If I am wrong then please let me know, and in case you have some pointers then do let me know that as well. Thanks PanB

          1 Reply Last reply
          0
          • P PankajB

            Hi All. A very simple question, but so far not able to find any concrete solution. I just want to round a decimal value to a provided decimal place. I have found few functions to do the same, but they also fail for some double values...like...

            double Round1(double dbVal, int nPlaces)
            {
            const double dbShift = pow(10.0, nPlaces);
            return floor(dbVal * dbShift + 0.5) / dbShift;
            }

            double Round2(double value,int pos)
            {
            double returnValue;
            double tens = exp(pos*log(10.0));
            if((value - floor(value*tens)/tens)*tens >= 0.5)
            returnValue = ceil(value*tens)/tens
            else
            returnValue = floor(value*tens)/tens;

            return returnValue;
            

            }

            Both of above function fails in case the double value is 10430.889999999999 464.45999999999998 3294.5100000000002 Please suggest. Thanks PanB

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

            Have a look here[^] to see how a double variable is stored in memory. You miss a ';' in your Round2 function, but otherwise the rounding functions are fine. 64 bits just isn't accurate enough to store the numbers you need.

            1 Reply Last reply
            0
            • P PankajB

              Hi All. A very simple question, but so far not able to find any concrete solution. I just want to round a decimal value to a provided decimal place. I have found few functions to do the same, but they also fail for some double values...like...

              double Round1(double dbVal, int nPlaces)
              {
              const double dbShift = pow(10.0, nPlaces);
              return floor(dbVal * dbShift + 0.5) / dbShift;
              }

              double Round2(double value,int pos)
              {
              double returnValue;
              double tens = exp(pos*log(10.0));
              if((value - floor(value*tens)/tens)*tens >= 0.5)
              returnValue = ceil(value*tens)/tens
              else
              returnValue = floor(value*tens)/tens;

              return returnValue;
              

              }

              Both of above function fails in case the double value is 10430.889999999999 464.45999999999998 3294.5100000000002 Please suggest. Thanks PanB

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

              You won't be able to do something like that due to the nature of floating points. I suggest you google for floating point precision for more information about the subject (e.g. on wikipedia[^]). The good news is that you most probably don't have to care about it. Why do you need to be precise like that ? Most of the time a such a precision error is perfectly acceptable.

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

              1 Reply Last reply
              0
              • _ _AnsHUMAN_

                Is it a possibility for you to convert this to CString (if using MFC) or char* (using sprintf_s)? If affirmative, use the format specifiers and call API CString::Format or sprintf_s and then convert back to double value when returning from your function.

                I am a HUMAN. I have that keyword in my name........ ;-)_AnsHUMAN_

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

                That's still won't solve the problem, you would still be limited by floating point precision.

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

                1 Reply Last reply
                0
                • P PankajB

                  Hi All. A very simple question, but so far not able to find any concrete solution. I just want to round a decimal value to a provided decimal place. I have found few functions to do the same, but they also fail for some double values...like...

                  double Round1(double dbVal, int nPlaces)
                  {
                  const double dbShift = pow(10.0, nPlaces);
                  return floor(dbVal * dbShift + 0.5) / dbShift;
                  }

                  double Round2(double value,int pos)
                  {
                  double returnValue;
                  double tens = exp(pos*log(10.0));
                  if((value - floor(value*tens)/tens)*tens >= 0.5)
                  returnValue = ceil(value*tens)/tens
                  else
                  returnValue = floor(value*tens)/tens;

                  return returnValue;
                  

                  }

                  Both of above function fails in case the double value is 10430.889999999999 464.45999999999998 3294.5100000000002 Please suggest. Thanks PanB

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

                  You cannot alter the precision of floating point numbers this way. Your rounding should occur when you wish to display the number as a string. A Google search for "floating point value" will find lots of papers that explain why this is so.

                  I must get a clever new signature for 2011.

                  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