Since this question gets asked about every week, this FAQ might help a lot of users.
How to convert an integer to a string in C++
how to convert a string into an integer in C++
how to convert a floating-point number to a string in C++
how to convert a string to a floating-point number in C++
Update for C++11
As of the
C++11
standard, string-to-number conversion and vice-versa are built in into the standard library. All the following functions are present in<string>
(as per paragraph 21.5).string to numeric
Each of these take a string as input and will try to convert it to a number. If no valid number could be constructed, for example because there is no numeric data or the number is out-of-range for the type, an exception is thrown (
std::invalid_argument
orstd::out_of_range
).If conversion succeeded and
idx
is not0
,idx
will contain the index of the first character that was not used for decoding. This could be an index behind the last character.Finally, the integral types allow to specify a base, for digits larger than 9, the alphabet is assumed (
a=10
untilz=35
). You can find more information about the exact formatting that can parsed here for floating-point numbers, signed integers and unsigned integers.Finally, for each function there is also an overload that accepts a
std::wstring
as it's first parameter.numeric to string
These are more straightforward, you pass the appropriate numeric type and you get a string back. For formatting options you should go back to the C++03 stringsream option and use stream manipulators, as explained in an other answer here.
As noted in the comments these functions fall back to a default mantissa precision that is likely not the maximum precision. If more precision is required for your application it's also best to go back to other string formatting procedures.
There are also similar functions defined that are named
to_wstring
, these will return astd::wstring
.In C++17, new functions std::to_chars and std::from_chars are introduced in header charconv.
From std::to_chars, same for std::from_chars.
Although it's not fully implemented by compilers, it definitely will be implemented.
I stole this convienent class from somewhere here at StackOverflow to convert anything streamable to a string:
And then you use it as;
Quite nifty!
Also I use this function to convert strings to anything streamable, althrough its not very safe if you try to parse a string not containing a number; (and its not as clever as the last one either)
Use as:
You might also want versions for wstrings.
How to convert a number to a string in C++03
itoa
oritof
functions because they are non-standard and therefore not portable.Use string streams
Note that you can use string streams also to convert floating-point numbers to string, and also to format the string as you wish, just like with
cout
You can use stream manipulators, such as
std::endl
,std::hex
and functionsstd::setw()
,std::setprecision()
etc. with string streams in exactly the same manner as withcout
Do not confuse
std::ostringstream
withstd::ostrstream
. The latter is deprecatedUse boost lexical cast. If you are not familiar with boost, it is a good idea to start with a small library like this lexical_cast. To download and install boost and its documentation go here. Although boost isn't in C++ standard many libraries of boost get standardized eventually and boost is widely considered of the best C++ libraries.
Lexical cast uses streams underneath, so basically this option is the same as the previous one, just less verbose.
How to convert a string to a number in C++03
The most lightweight option, inherited from C, is the functions
atoi
(for integers (alphabetical to integer)) andatof
(for floating-point values (alphabetical to float)). These functions take a C-style string as an argument (const char *
) and therefore their usage may be considered a not exactly good C++ practice. cplusplus.com has easy-to-understand documentation on both atoi and atof including how they behave in case of bad input. However the link contains an error in that according to the standard if the input number is too large to fit in the target type, the behavior is undefined.Use string streams (this time input string stream,
istringstream
). Again, istringstream is used just likecin
. Again, do not confuseistringstream
withistrstream
. The latter is deprecated.Use boost lexical cast.
In case of a bad input,
lexical_cast
throws an exception of typeboost::bad_lexical_cast