how to convert a String to Number or Number to String

hi to every one

how to convert a String to Number or Number to String??

i cat use toString !!:(

any body can help me PLZ :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string inttostr(int integer) {
	ostringstream MyStream;
	MyStream<<integer<<flush;
	return (MyStream.str());
}

int strtoint(string String) { //can be modified to make better, but this should work for simple stuff
	int a;
	stringstream stream;
	for(int b = 0; b < String.size(); b++) {
		if(!isdigit(String.at(b))) {
			String.erase(b, 1);
		}
	}
	stream<<String<<flush;
	stream>>a;
	return a;
}
Last edited on
or u can use atoi and itoa search it in the searchbox on this website
u can use also sscanf and sprintf.
I think atoi isn't ANSI and so isn't supported by all compilers.
I use :
 
sprintf(buffer,"%d",val);
You could also use the boost libraries (www.boost.org).

1
2
3
4
5
6
7
8
#include <boost/lexical_cast.hpp>

string intAsString( "123" );
try {  
    int a = boost::lexical_cast<int>( intAsString );
} catch { boost::bad_lexical_cast const& ) {
    std::cerr << "Value was not convertible to int" << std::endl;
}

hi everybody

I'm using a method similar to firedraco's to convert a string to a float, perform calculations with it, then convert my float result to a string that i display in an Edit Control.

Does anyone know a function that could limit the number of decimal points of my float result to 3 before i convert it to a string to be displayed in the Edit Control?
You need to set the stream to fixed notation then set the precision to 3.
This will mean you always get 3 decimal places - so 0.5 would display as 0.500.
See http://www.cplusplus.com/reference/iostream/ostringstream/ for docs on the syntax for setting precision and fixed notation.
Topic archived. No new replies allowed.