| Bazzy (6281) | |||||||||
|
This question is asked quite often, so here is a way of doing it using stringstreams: number to string
string to number
Simple functions to do these conversions
Other ways: Boost http://www.boost.org/doc/libs/1_38_0/libs/conversion/lexical_cast.htm C library http://www.cplusplus.com/reference/clibrary/cstdio/sprintf.html http://www.cplusplus.com/reference/clibrary/cstdlib/itoa.html http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html http://www.cplusplus.com/reference/clibrary/cstdlib/atol.html http://www.cplusplus.com/reference/clibrary/cstdlib/atof.html http://www.cplusplus.com/reference/clibrary/cstdlib/strtol.html http://www.cplusplus.com/reference/clibrary/cstdlib/strtoul.html http://www.cplusplus.com/reference/clibrary/cstdlib/strtod.html ( I explain better with code examples than with words ) | |||||||||
|
Last edited on
|
|||||||||
| firedraco (5495) | |
Looks good...but I have a question...does ss >> result filter out "junk" characters, i.e. if you put in "9kf84.85" into a double will it give you 984.85 or not do anything (giving you 0).Also, 0 could theoretically be a valid value for what they are putting in...I would either ask them to specify a default value to return or throw an exception. | |
|
|
|
| Bazzy (6281) | |||
"9kf84.85" becomes 9
| |||
|
|
|||
| Bazzy (6281) | |||
A better string to number function:
leaves only numeric characters, sign and scientific notation (for doubles) so that StringToNumber("3 ran 8 dom 9 chars 0.45e+3",0.0) will produce 3890450
| |||
|
Last edited on
|
|||
| Tilpo (8) | |
| And how would a function that returns a default value if any text except for mathimatical operators are found look like? | |
|
|
|
| Bazzy (6281) | |
|
The last version I provided leaves + and - in the string before converting it to a number (as a number can be in the form +123.456e-7), if you leave in the string other characters the result would be truncated eg: "123/456" = 123 For the edits above: I've just noticed that I forgot to pass the string as reference | |
|
Last edited on
|
|
| namelij (3) | |
|
int result; string text("123"); stringstream convert(text); if(!(convert >> text)) result = 0; cout << result; why my answer is 3688360, when the text is "123" or "456" | |
|
Last edited on
|
|
| Bazzy (6281) | |||||
Your error is here:
| |||||
|
|
|||||
| helios (10258) | |||
Well, I've been using these, which've worked quite well, for me:
| |||
|
|
|||
| namelij (3) | |
|
Bazzy, thank you! | |
|
|
|
| chiwing (154) | |||
i encount compile error of it how to solve it? | |||
|
Last edited on
|
|||
| Bazzy (6281) | |
| You need to declare 'convert' before using it | |
|
|
|
| chiwing (154) | |||
after adding stringstream convert; still have error how to amend~ thanks | |||
|
Last edited on
|
|||
| Bazzy (6281) | |
| Did you #include<sstream> ? | |
|
|
|
| jsmith (5804) | |
|
And need std::stringstream too | |
|
|
|
| Duoas (6752) | |
| Bazzy +1 | |
|
|
|
| chiwing (154) | |
| may i ask how to convert number 324545678 to wstring ? | |
|
|
|
| Bazzy (6281) | |||
try with wstringstream:
| |||
|
Last edited on
|
|||