可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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);
回答2:
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())
.
回答3:
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.