C++ C++;MFC双到CString

C++ C++;MFC双到CString,c++,type-conversion,C++,Type Conversion,对不起我的英语 我需要将double值转换为CString,因为我需要执行AfxMessageBox(double_值) 我发现: std::ostringstream ost; ost << double_value; std::cout << "As string: " << ost.str() << std::endl; //AfxMessageBox(ost.str()); - Does not work. std::ostringstr

对不起我的英语

我需要将double值转换为CString,因为我需要执行AfxMessageBox(double_值)

我发现:

std::ostringstream ost;
ost << double_value;
std::cout << "As string: " << ost.str() << std::endl;
//AfxMessageBox(ost.str()); - Does not work.
std::ostringstream-ost;

OST

这是因为OST.String()不是cstring,而是C++字符串对象。您需要将其转换为CString:

new-CString(ost.str())
AfxMessageBox
需要一个
CString
对象,因此将双精度设置为
CString
格式并传递:

CString str;
str.Format("As string: %g", double);
AfxMessageBox(str);
编辑:如果要将值显示为整数(小数点后无值),请使用此选项:

str.Format("As string: %d", (int)double);

根据您需要的Unicode设置

std::ostringstream ost;
ost << std::setprecision(2) << double_value;
std::cout << "As string: " << ost.str() << std::endl;
AfxMessageBox(ost.str().c_str());
std::ostringstream-ost;

ost当然不起作用-AfxMessageBox需要一个双精度,为什么它应该能够获得cstring?AfxMessageBox不需要双精度…@zmbq不需要AfxMessageBox(双精度值);不建议它需要一个双字符?AfxMessageBox需要CString:AfxMessageBox(“文本”);事实上,AfxMessageBox需要的不是CString,而是LPCTSTR,但后者有很好的隐式转换,因此可以工作。您的试用意味着您有UNICODE配置,这是导致错误的原因。您可以使用AfxMessageBoxA而不是AfxMessageBox。当然,使用接受的答案也可以。对不起,我是初学者CString sss=new CString(ost.str());不是工作大thx,它是工作。请,如果你知道-告诉我如何裁剪/切割CString的一部分。我有12.09个值,我需要12个值。使用
%d
而不是
%g
,它应该给你整数值。%d给我这个:作为字符串:-1546188226@user2254511你的问题解决了吗?(关键是转换到
int
)。
std::wostringstream ost;
ost << std::setprecision(2) << double_value;
std::wcout << L"As string: " << ost.str() << std::endl;
AfxMessageBox(ost.str().c_str());