大家都知道 :cout.setf()是用来设置位的,cout.precision()是用来设置精度的,但具体如何操作想必难倒一批人!
std::ios_base::setf或者 std::ios::setf
C++ Input/output library std::ios_base
fmtflags setf( fmtflags flags ); | (1) | |
fmtflags setf( fmtflags flags, fmtflags mask ); | (2) | |
Sets the formatting flags to specified settings.
1) Sets the formatting flags to flags
2) Clears the formatting flags under
mask
, and sets the cleared flags to those specified by flags
. Essentially the following operation is performed(flags & mask) where fl
defines the state of internal formatting flags.Parameters
flags, mask | - | new formatting setting. mask defines which flags can be altered, flags defines which flags of those to be altered should be set (others will be cleared). Both parameters can be a combination of the following constants:
|
【cout.setf() 与 cout.precision()】例子:
C/C++ code?
1 2 3 4 5 6 7 8 9 10 11 | #include using namespace std;
int main () { cout.setf ( ios::hex, ios::basefield );
// set hex as the basefield cout.setf ( ios::showbase );
// activate showbase cout << 100 << endl;
cout.setf ( 0, ios::showbase );
// deactivate showbase cout << 100 << endl;
return 0;
} |
输出:
0x64
64
setprecision:
C/C++ code?
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include #include using namespace std;
int main () { double f =3.14159;
cout << setprecision (5) << f << endl;
cout << setprecision (9) << f << endl;
cout << fixed;
cout << setprecision (5) << f << endl;
cout << setprecision (9) << f << endl;
return 0;
} |
输出:
3.1416
3.14159
3.14159
3.141590000
参考网址:
http://en.cppreference.com/w/cpp/io/ios_base/setf
推荐阅读
- 十万个为什么|口诀快速记住C语言中的运算符优先级
- bug|一次由于传递局部变量指针产生的bug
- linux|linux进程、线程常用的几个基础知识点
- C|C语言——知识点汇总
- c/c++技术|小白 C++ 入门到大神发疯学习路线
- C++|C++应用程序列表(来自Bjarne Stroustrup)
- C|梦回童年——基于C语言实现三子棋小游戏
- 数据结构|数据结构 链表 合并两个有序的单链表 C语言版
- C/C++|二维数组与数组指针详解