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

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

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

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

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.

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.

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

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);

}

the c++-standard-header cmath doesn't have it as well,

i guess it's a microsoft-class as wayside said above....

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

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(tem

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();

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.

outstream.precision((int)(

Or in your case,

ssConvert.precision((int)(

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);

}

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 trialThanks, exactly what I was looking for. Excellent, simple solution that is working great for all cases.

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.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

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

ssConvert << (dblValue + 0.005f);