C++ MFC double to CString
C++ MFC double to CString
对不起我的英语。
我需要将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
对象,因此将double格式化为CString
并传递:
CString str;
str.Format("As string: %g", double);
AfxMessageBox(str);
编辑:如果您希望值显示为整数(小数点后没有值),则使用以下内容:
str.Format("As string: %d", (int)double);
new CString(ost.str())
。
根据您的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());
这是必需的,因为CString有一个const char*
或const wchar_t*
的构造函数。没有std::string或std::wstring的构造函数。您也可以使用CString.Format,它有类似sprintf的非类型保存问题。
请注意,双重转换取决于区域设置。小数分隔符将取决于您的位置
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++ Singleton - Prevent ::instance() to variable
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 在C++中使用Cstring反转字符串
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- Python str to C++ to Python str
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- CString to std::string or sql::SQLString conversion - C++
- CString to Float conversion
- CString to TCHAR
- memcpy CString to char*
- CString to LPWSTR in mfc
- CString to std::cout
- C++ MFC double to CString