convertion between string and double
-
I have a problem with convertion between string and double as following: double dValue = 1000; std::ostrstream oStrStream; oStrStream << dValue << std::ends; std::string strValue = oStrStream.str(); //here strValue is "1,000", a comma is added! //when I use following code to convert, error occurred. double dValue = strtod( strValue .c_str(), NULL ); //here dValue is "1", it seems "," can't be recognized! How to avoid this? Is there any way to avoid adding comma when convert double to string?
-
I have a problem with convertion between string and double as following: double dValue = 1000; std::ostrstream oStrStream; oStrStream << dValue << std::ends; std::string strValue = oStrStream.str(); //here strValue is "1,000", a comma is added! //when I use following code to convert, error occurred. double dValue = strtod( strValue .c_str(), NULL ); //here dValue is "1", it seems "," can't be recognized! How to avoid this? Is there any way to avoid adding comma when convert double to string?
ostrstream
is not supported anymore. Try usingostringstream
;«_Superman_» _I love work. It gives me something to do between weekends.
-
ostrstream
is not supported anymore. Try usingostringstream
;«_Superman_» _I love work. It gives me something to do between weekends.
I got the same result. The problem was not solved yet.
-
I have a problem with convertion between string and double as following: double dValue = 1000; std::ostrstream oStrStream; oStrStream << dValue << std::ends; std::string strValue = oStrStream.str(); //here strValue is "1,000", a comma is added! //when I use following code to convert, error occurred. double dValue = strtod( strValue .c_str(), NULL ); //here dValue is "1", it seems "," can't be recognized! How to avoid this? Is there any way to avoid adding comma when convert double to string?
You need to parse the string to remove non-numeric characters before trying to convert it. The string is only allowed to follow a specific pattern, as described here[^].
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
I got the same result. The problem was not solved yet.
The problem is due to ',' in '1,000'. Remove ',' and it would work.
-
You need to parse the string to remove non-numeric characters before trying to convert it. The string is only allowed to follow a specific pattern, as described here[^].
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
I could be wrong here, didn't verify yet, but using the setlocale to LC_NUMERIC for US English shoul make it recognize the thousand seperator as well. Always consider that there are languages (mine included) that use a different decimal seperator. Cheers, AT
Cogito ergo sum
-
I could be wrong here, didn't verify yet, but using the setlocale to LC_NUMERIC for US English shoul make it recognize the thousand seperator as well. Always consider that there are languages (mine included) that use a different decimal seperator. Cheers, AT
Cogito ergo sum
If you look at the link I provided you can see which characters may be accepted.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman