Mad*_*adz 2 c++ string boost visual-c++
我是C++的新手,正在努力学习一段代码.我在对话框中有一个静态文本,我想在按钮点击时更新.
double num = 4.7;
std::string str = (boost::lexical_cast<std::string>(num));
test.SetWindowTextA(str.c_str());
//test is the Static text variable
Run Code Online (Sandbox Code Playgroud)
但是,文本显示为4.70000000000002.我如何使它看起来像4.7.
我用过.c_str(),否则cannot convert parameter 1 from 'std::string' to 'LPCTSTR'会抛出错误.
使用在c_str()这里是正确的.
如果您想更好地控制格式,请不要boost::lexical_cast自己使用和实现转换:
double num = 4.7;
std::ostringstream ss;
ss << std::setprecision(2) << num; //the max. number of digits you want displayed
test.SetWindowTextA(ss.str().c_str());
Run Code Online (Sandbox Code Playgroud)
或者,如果您需要字符串,而不是将其设置为窗口文本,如下所示:
double num = 4.7;
std::ostringstream ss;
ss << std::setprecision(2) << num; //the max. number of digits you want displayed
std::string str = ss.str();
test.SetWindowTextA(str.c_str());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2006 次 |
| 最近记录: |