输入格式控制:C++的cin自动忽略换行、空格、回车。
输出格式控制:利用cout的输出控制符
例如:setprecision(n)tmep
保留n位有效数字
int main()
{
double a = 123.4444555666;
cout << setprecision(7); //保留7位有效数字
cout << a << endl;
/*
Output:
123.4445
*/
return 0;
}
配合setiosflags(ios::fixed)
使用,小数点后保留n位小数
int main()
{
double a = 123.4444555666;
cout << setprecision(7) << setiosflags(ios::fixed); //小数点后保留7位小数
cout << a << endl;
/*
Output:
123.4444556
*/
return 0;
}
需要注意的是,设置了一次操作符,对后面的输出默认生效,除非取消或者更改,其他的一些操作符详见参考资料。