- cout.setf
- cout.precision
std::ios_base提供了一系列格式化标志,可以用于控制输出的格式,这些格式可以用setf和unsetf方法设置和清除;
常见的格式化标志
std::ios_base::dec
:十进制格式(默认)。
std::ios_base::hex
:十六进制格式。
std::ios_base::oct
:八进制格式。
std::ios_base::fixed
:固定小数点格式。
std::ios_base::scientific
:科学计数法格式。
std::ios_base::left
:左对齐。
std::ios_base::right
:右对齐(默认)。
std::ios_base::internal
:内部对齐,用于数值的符号或基数前缀。
std::ios_base::boolalpha
:布尔值以 true
和 false
输出。
std::ios_base::noboolalpha
:布尔值以 1
和 0
输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include <iostream>
int main() { int value = 15;
std::cout << std::hex << value << std::endl;
std::cout << std::dec << value << std::endl;
double pi = 3.14159265358979323846; std::cout << std::fixed << pi << std::endl;
std::cout << std::scientific << pi << std::endl;
std::cout << std::left << std::setw(10) << value << std::endl;
std::cout << std::right << std::setw(10) << value << std::endl;
bool flag = true; std::cout << std::boolalpha << flag << std::endl; std::cout << std::noboolalpha << flag << std::endl;
return 0; }
|
精度设置
精度设置用于控制浮点数输出的小数位数,可以使用std::setprecision操作符来设置精度;
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <iostream> #include <iomanip>
int main() { double pi = 3.14159265358979323846;
std::cout << std::fixed << std::setprecision(3) << pi << std::endl;
std::cout << std::fixed << std::setprecision(5) << pi << std::endl;
return 0; }
|
宽度设置
宽度设置用于控制输出字段的宽度,可以使用std::setw操作符来设置宽度;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <iostream> #include <iomanip>
int main() { int value = 15;
std::cout << std::setw(10) << value << std::endl;
std::cout << std::left << std::setw(10) << value << std::endl;
return 0; }
|
填充字符设置
填充字符设置用于控制输出字段的填充字符,可以使用std::setfill操作符来设置填充字符;
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <iostream> #include <iomanip>
int main() { int value = 15;
std::cout << std::setw(10) << std::setfill('*') << value << std::endl;
std::cout << std::left << std::setw(10) << std::setfill('*') << value << std::endl;
return 0; }
|
保存和恢复格式状态
某些情况下,需要保存当前的格式状态,进行一些特定的格式化输出,然后再恢复原来的格式状态,可以使用std::ios_base::fmtflags和std::streamsize来保存和恢复格式状态;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include <iostream> #include <iomanip>
using std::ios_base; using std::cout; using std::endl; using std::setprecision; using std::fixed;
ios_base::fmtflags saveFormat(cout.flags()); std::streamsize savePrec(cout.precision());
cout << fixed << setprecision(2);
double pi = 3.14159265358979323846; cout << "Pi: " << pi << endl;
cout.flags(saveFormat); cout.precision(savePrec);
cout << "Pi: " << pi << endl;
return 0;
|