views:

644

answers:

4

From managed c++, I am calling an unmanaged c++ method which returns a double. How can I convert this double into a managed string?

Sorry if this is a stupid question, I'm still very new with C++ in .net.

Thanks!

+1  A: 

C++ is definitely not my strongest skillset. Misread the question, but this should convert to a std::string, not exactly what you are looking for though, but leaving it since it was the original post....

double d = 123.45;
std::ostringstream oss;
oss << d;
std::string s = oss.str();

This should convert to a managed string however..

double d = 123.45
String^ s = System::Convert::ToString(d);
Scott Nichols
+5  A: 

I assume something like

(gcnew System::Double(d)).ToString()
DrPizza
Short and concised, thanks! Note: It should be (gcnew System::Double(d))->ToString() instead, but close enough.
stung
A: 

If you want a standard char* string, you can use sprintf. You just need to make sure the char* is sufficiently large.

double d = 123.45;
char str[40];
sprintf(str, "%f", d);
Kip
A: 

He said "managed string". Stop talking about char*s or std::strings, and stop downvoting me for actually trying to do something with managed strings.

DrPizza
Yeah, I misread the original question, just read it as convert double to string. Most of my work is with C# not C++, my edit should work though right?
Scott Nichols
Oh, yeah, that looks like it should work.
DrPizza