数据精度,位宽设置
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
double d = sqrt(2.0);
cout << "精度设置: " << endl;
for (int i = 0; i < 5; i++)
{
cout << setprecision(i) << d << endl; // 设置不同的精度
}
cout << "当前的精度为: " << cout.precision() << endl;
cout << "当前域宽: " << cout.width() << endl;
cout << setw(6) << d << endl; // 默认右对齐
cout << "当前填充字符:" << endl;
cout << setfill('*') << setw(10) << d << endl; // 通过setfill() 函数可以直接插入流
return 0;
}
oct: 八进制格式输出数据
dec: 十进制格式输出数据
hex: 十六进制格式输出数据
endl: 插入换行符并刷新输出缓冲流
uppercase: 在以十六进制格式输出时字母大写
skipws: 在输出时跳过空白
flush: 刷新流
left: 左对齐
right: 右对齐
scientific: 以科学计数法输出
fixed: 以定点数方式输出
showbase: 输出前缀(八进制0, 十六进制0x)
showpoint: 在输出浮点数时带小数点
showpos: 在输出正整数时加'+'