开发者

converting String to double in vc++

Can anyone help me with a way of converting String to do开发者_高级运维uble in vc++?

I can't use atoi, since it converts char to double. But I am using istringstream.

std::istringstream stm; 

double d;
String name = "32.67";

stm.str(name);
stm >>d; 

It will give a compilation error:

error C2664: 'void std::basic_istringstream::str(const std::basic_string &)' :
cannot convert parameter 1 from 'System::String ^' to 'const std::basic_string &'

Please help with different solution or correct this.


std::stringstream str() accepts a std::string as an argument. You're passing it a System::String, wherever that comes from. Given the funky ^ symbol you must be using C++/CLI, using .NET strings.

Use std::string unless you are for some reason required to use the .NET library, in which case you need to either use .NET conversion functions, or convert to a std::string (or char* c-string and use the << operator).


As the other responder has suggestion, you are probably using C++/CLI. In that case:

 String ^ name = "32.67";
 double d;
 d = Double::Parse(name);

Note that if the string cannot be parsed into a double, an exception will be thrown. Use Double::TryParse if you want to avoid that (it returns false if the string cannot be parsed).


I think it is very simple in vc++/CLR programming.

String ^name = "32.56";
String ^no = "56";

Double number_double = Convert::ToDouble(name); // convert String to double
Int number_int = Convert::ToInt32(no); // convert String to integer
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜