If you would convert these 3 examples to a string you would get an errormessage as no of the numbers are valid numbers.

How would it be possible to do a check programatically if the number is a valid number before a conversion ?

String^ GetValue = "";
GetValue = Convert::ToDouble(0..25); //This will not be a valid number
GetValue = Convert::ToDouble(0./25); //This will not be a valid number
GetValue = Convert::ToDouble(0..=25);//This will not be a valid number

I think you have made a mistake in your question. If you have
0..25 in your code your compiler will tell you that it is an error.
However, if you are trying to go from a string to a number the simplest way is to use a stringstream.

The problem with

std::istringstream IX;
double x;
IX>>x;

is that as you say 0.. and 34y will work. So you have to do some more work.

You need to (a) determine if the stream is emtpy or (b) determine if the stream's next character is a space (or other suitable separator). Either (a) or (b) would mean an acceptable number had been read.

Thanks.. I supposed I needed to do such a check to determine that all characters is okay. I will do that and see if it will work out okay..


I think you have made a mistake in your question. If you have
0..25 in your code your compiler will tell you that it is an error.
However, if you are trying to go from a string to a number the simplest way is to use a stringstream.

The problem with

std::istringstream IX;
double x;
IX>>x;

is that as you say 0.. and 34y will work. So you have to do some more work.

You need to (a) determine if the stream is emtpy or (b) determine if the stream's next character is a space (or other suitable separator). Either (a) or (b) would mean an acceptable number had been read.

Hi *,

I have been trying to convert a string to a double but I can not get all the digits, for example :

string g = "345.3456788";

I tried this :

string g = "345.3456788";
double e;

e = atof(g);

and

stringstream aux;
string number = "345.3456788";
double result;

aux.str(number);
aux >> result;

But the result is a rounded number and I do not want a rounded number, I want the number as it is.

Am I doing something wrong?,

Thanks in advanced. Have a very nice day.

Hi * again,

I found setprecision() and fixes flags, my problem was not the conversion per se, it was in cout :P

Thanks ans sorry to reopen a solved thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.