使用 ostringstream
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float s = 6.7592;
cout << to_string(s) << endl; // 6.759200
double k = 6.7592;
cout << to_string(k) << endl; // 6.759200
int i = 923342;
cout << to_string(i) << endl; // 923342
ostringstream strStream;
strStream << s;
cout << strStream.str() << endl; // 6.7592
return 0;
}
本文介绍了如何使用C++中的ostringstream类进行数据类型转换,通过实例展示了如何将浮点数、双精度数和整数转换为字符串。这在需要将数值数据以特定格式输出到文件或显示在界面上时非常有用。
3413

被折叠的 条评论
为什么被折叠?



