tha*_*hgr 1 c++ string double decimal
我一直试图将一个double转换为没有C++ 11(ToString)的字符串,但它只接受小数位数作为默认值为5.我怎么能改变呢?
命令是:
string a = static_cast<ostringstream*>( &(ostringstream()<<digits) )->str();
Run Code Online (Sandbox Code Playgroud)
但它保持5位小数,而我想创建一个包含所有小数的字符串(例如100)我知道,那么多小数并不重要.这是我正在做的一个练习点.
有什么建议?
非常感谢你的时间干杯!
在这里使用IO操纵器setprecisionstd::cout但是可以工作stringstream:
// setprecision example from cplusplus.com
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
int main () {
double f =3.14159;
std::cout << std::fixed << std::setprecision(9) << f << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,没有double100个有意义的数字,它是15或17,我忘了究竟有多少.
编辑:我忘了,如果你可以使用C++ 11 ......你可以(而且应该)使用to_string
#include <string>
// ....
std::string f_str = std::to_string(f);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2648 次 |
| 最近记录: |