Question! Converting String to a double or Integer where the value is rounded up or down?

Hi guys,

My question is about how, when received a string& one can take that string and convert it to double,
alternatively I want the string to be converted into an integer rounded up or down ie value in from string is 1.560008 then the int = 2 etc.

Here's how I failed to convert the string& to double:

void getValue(string& theValue) {
      double d;
      char l [256];
      l = theValue;
      d = atof(theValue);
      cout << d << endl;
      cout << flush;

any inputs are welcomed

All the best
Who is Participating?
nafis_devlprConnect With a Mentor Commented:
you can do it this way

 d = atof(theValue.c_str());


sscanf(theValue.c_str(), "%lf", &d);

for the second question, do the above then do this

int i= (int)(d+0.5);

hope this helps

>> int i= (int)(d+0.5);

this will actually round the values, for example :

if 1.5 it will be 2
if 1.4 it will be 1

you can also use the ceil or floor to round-up or round-down the values, for exmple

if  1.5
then ceil(1.5) will give 2.0
and floor(1.5) will give 1.0

IdarhillgaarAuthor Commented:
Fabulous answers - Have a good summer!

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.

All Courses

From novice to tech pro — start learning today.