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

Posted on 2006-07-24
Last Modified: 2010-04-01
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
Question by:Idarhillgaar
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

nafis_devlpr earned 250 total points
ID: 17166576
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


Expert Comment

ID: 17166592
>> 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


Author Comment

ID: 17166719
Fabulous answers - Have a good summer!


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question