Reputation: 123
Sorry for my English.
I need to convert double value to CString, because i need to do AfxMessageBox(double_value);
I find this:
std::ostringstream ost;
ost << double_value;
std::cout << "As string: " << ost.str() << std::endl;
//AfxMessageBox(ost.str()); - Does not work.
How i can do this?
Upvotes: 4
Views: 27312
Reputation: 4366
Depending on your Unicode settings you need
std::ostringstream ost;
ost << std::setprecision(2) << double_value;
std::cout << "As string: " << ost.str() << std::endl;
AfxMessageBox(ost.str().c_str());
or
std::wostringstream ost;
ost << std::setprecision(2) << double_value;
std::wcout << L"As string: " << ost.str() << std::endl;
AfxMessageBox(ost.str().c_str());
This is needed because CString has a constructor for const char*
or const wchar_t*
. There is no constructor for std::string or std::wstring. You can also use the CString.Format which has the same not typesave problems like sprintf.
Be aware that double conversion is locale dependent. Decimal seperator will depend on your location.
Upvotes: 0
Reputation: 122391
AfxMessageBox
expects a CString
object, so format the double into a CString
and pass that:
CString str;
str.Format("As string: %g", double);
AfxMessageBox(str);
Edit: If you want the value displayed as an integer (no value after decimal point) then use this instead:
str.Format("As string: %d", (int)double);
Upvotes: 14
Reputation: 39023
That's because ost.str() is not a CString, but rather a C++ string object. You need to convert that to CString: new CString(ost.str())
.
Upvotes: 0