#include <iostream>
#include <ostream>
using namespace std;
int main()
{
// setiosflags(ios::fixed)是指以固定的浮点显示
// 当setiosflags(ios::fixed)和serprecision(n)两个一起用时就表示保留n位小数输出
// setprecision是指设置输出精度,当没有setiosflags(ios::fixed)时,输出格式是数据的有效位数
string name = "abc.txt";
ofstream file(name, ios::out);
float a = 123.456;
// 此处设置,只针对file
file << setiosflags(ios::fixed);
file << setprecision(2);
// 此处设置,只针对cout
cout << setiosflags(ios::fixed);
cout << setprecision(5);
cout << a << endl;
file << a << endl;
getchar();
return 0;
}
cout的输出:
file保存的内容: