rounding question

I'm currently using microsoft visual c++ .net version 7.1.3088

I need to round a double value.
I tried the following code

                  stringstream ssConvert("");
                  // initially I retrieve the value from my class where it's a string
                  string tempStr = m_ColumnData.at(i);
                  double dblValue = atof(tempStr.c_str());
                  // now I try use Math rounding function
                  dblValue = Math::Round(dblValue, 2); // I need to round to 2 decimal places
                  ssConvert.precision(2);
                  ssConvert << dblValue;
                  m_ColumnData[i] = ssConvert;

When I compile, I get error C2653: 'Math' : is not a class or namespace name

I tried to
#include <cmath> or math.h and I'm getting the same compiler error.

Thanks in advance
healingtaoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

waysideCommented:
Math::Round is a .NET function, unless you are writing managed C++ code you can't use it.

For regular C++, one way would be to add 0.005 to the number before putting it to the stringstream.

 ssConvert << (dblValue + 0.005f);

0
waysideCommented:
You could also round it arithmetically:

dblValue = (  (int)((dblValue + 0.005f) * 100.0f) )  /100.0f;

as long as your original number *100 doesn't exceed a 32 bit integer.

Because of the internal representation of floating numbers, though, you might wind with

x.499999999999999999  instead of  x.50000000000000, which puts you right back where you started from... you'll have to try it and see it if works for your range of values.

0
pb_indiaCommented:
Have you tried adding the math.h header file to your code?  That might work, but not sure.  Math::Round(double, int) basically means that the first parameter (double) can be a number like 2.34785647 and the second parameter (int) tells how many decimal places to round off to. So Math::Round(2.34785647, 1) would return 2.3   Hope that this helps.  
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

ikeworkCommented:
or this way:

void round( double &dblVal )
{
    double dblTemp = floor( dblVal * 100.0f );
   
    dblVal *= 100.0;

    if( dblVal - dblTemp >= 0.5 ) dblVal = dblTemp + 1.0;
    else dblVal = dblTemp;

    dblVal /= 100.0;
}

good luck ;)
ikework
0
pb_indiaCommented:
This will handle all double's and you can spesify the number of digits.

double Round(double Value, int Precision)
{
     static const double Base = 10.0f;
     double Complete5, Complete5i;
     
     Complete5 = Value * pow(Base, (double) (Precision + 1));
     
     if(Value < 0.0f)
          Complete5 -= 5.0f;
     else
          Complete5 += 5.0f;
     
     Complete5 /= Base;
     modf(Complete5, &Complete5i);
     
     return Complete5i / pow(Base, (double) Precision);
}
0
ikeworkCommented:
@pb_india: the standard-c-header math.h doesn't have any class Math, it's a c-header... no classes,
the c++-standard-header cmath doesn't have it as well,
i guess it's a microsoft-class as wayside said above....
0
healingtaoAuthor Commented:
pb_india ,

I like  the code you provided but I have a slight problem with it.
Here is the sample I'm using to test this

#include "stdafx.h"
#include <math.h>
#include <sstream>
#include <fstream>
#include <string>

double roundVal(double Value, int Precision);
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
      double dblValue;
      stringstream ssConvert("");
      string tempStr = "12343.234233445";
      dblValue = atof(tempStr.c_str());
      dblValue = roundVal(dblValue, 6);
      ssConvert.precision(6);
      ssConvert << dblValue;
      string finalVal = ssConvert.str();

      return 0;
}

double roundVal(double Value, int Precision)
{
     static const double Base = 10.0f;
     double Complete5, Complete5i;
     
     Complete5 = Value * pow(Base, (double) (Precision + 1));
     
     if(Value < 0.0f)
          Complete5 -= 5.0f;
     else
          Complete5 += 5.0f;
     
     Complete5 /= Base;
     modf(Complete5, &Complete5i);
     
     return Complete5i / pow(Base, (double) Precision);
}

When I use precision 6 in this case my finalVal is 12343.2 but I need 12343.234233
When I increase precision say to 20 I thought that it'll fix the problem but I get "12343.234233445". I thought precision only counts decimals but apparently it's the whole number. I need something generic because I have a lot of big values with up to 9 decimal places.
Does it make sense  to take count the real number part (in this case 5) and add number of decimals(in this case 6) , add them and set precision(11) ???
Or is there an easier way?

Thanks
0
pb_indiaCommented:
healingtao ,

Problem is in your code. The function I provided is fine.
Preision 1 would round it to 1 after decimal
Precision 2 will round it to 2 digits after decimal

See below: The commments on cout

double dblValue=0;
     stringstream ssConvert("");
        dblValue = 123.43234233445;//atof(tempStr.c_str());
  cout<<dblValue<<endl;  //   [RESULTis 123.432...this is before the function is used]
     dblValue = roundVal(dblValue,2);
   cout<<dblValue;//  [RESULT is 123.4 /...after the rounding is done to 1 ]
     ssConvert.precision(6);
     ssConvert << dblValue;
     string finalVal = ssConvert.str();
0
esorfCommented:
Correction, pb_india.

Check out http://www.cplusplus.com/ref/iostream/ios_base/precision.html

Specifically:
  The precision determines the maximum number of digits that shall be output on insertion operations to express floating-point values, counting both the digits before and after the decimal point.
0
esorfCommented:
So, Author, if you want to get X digits of precision following decimal:

outstream.precision((int)(log10(dblValue))+1+X)

Or in your case,

ssConvert.precision((int)(log10(dblValue))+7);  // For max 6 digits after decimal
0
waysideCommented:
Sometimes the c runtime is the simplest...

char buf[50];
double dblVal = 1.234567;
sprintf(buf, "%.2f", dblVal); // sprintf takes care of rounding for you
dblVal = atof(buf);

if you want an arbitrary number of digits after the decimal point:

double round(double value, int precision) {
   char buf[50];
   sprintf(buf, "%.*f", precision, value); // sprintf rounds for you
  return atof(buf);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
healingtaoAuthor Commented:
wayside,

Thanks, exactly what I was looking for. Excellent, simple solution that is working great for all cases.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.