很抱歉我的英语不好。
我需要将double值转换为CString,因为我需要执行AfxMessageBox(double_value);
我找到了以下代码:
std::ostringstream ost;
ost << double_value;
std::cout << "As string: " << ost.str() << std::endl;
//AfxMessageBox(ost.str()); - Does not work.
我该怎么做呢?
很抱歉我的英语不好。
我需要将double值转换为CString,因为我需要执行AfxMessageBox(double_value);
我找到了以下代码:
std::ostringstream ost;
ost << double_value;
std::cout << "As string: " << ost.str() << std::endl;
//AfxMessageBox(ost.str()); - Does not work.
我该怎么做呢?
AfxMessageBox
需要一个 CString
对象,因此将双精度数格式化为 CString
,然后传递它:
CString str;
str.Format("As string: %g", double);
AfxMessageBox(str);
编辑:如果你想将值显示为整数(小数点后没有值),那么请使用以下内容:
str.Format("As string: %d", (int)double);
%d
代替%g
,这将为您提供整数值。 - olevegard根据您的Unicode设置,您需要
std::ostringstream ost;
ost << std::setprecision(2) << double_value;
std::cout << "As string: " << ost.str() << std::endl;
AfxMessageBox(ost.str().c_str());
或者
std::wostringstream ost;
ost << std::setprecision(2) << double_value;
std::wcout << L"As string: " << ost.str() << std::endl;
AfxMessageBox(ost.str().c_str());
const char*
或const wchar_t*
。没有std::string或std::wstring的构造函数。您还可以使用CString.Format,它具有与sprintf相同的非类型安全问题。
请注意,双重转换取决于区域设置。小数分隔符将取决于您的位置。
new CString(ost.str())
。