cout
文章目錄
- 1 cout的常用用法
- 1.1 設置輸出寬度
- 1.2 浮點數據的輸出控制
1 cout的常用用法
1.1 設置輸出寬度
需要包含頭文件:#include <iomanip>。
需要注意,設置僅對下一次輸出生效。用法如下:
int a = 100; cout << setw(8) << setiosflags(ios::left) << a << endl;1.2 浮點數據的輸出控制
#include<iostream> #include<Windows.h>using namespace std;int main(void){double value = 12.3456789;// 默認精度是6,所以輸出為 12.3457 //(默認情況下,精度是指總的有效數字)cout << value << endl; // 把精度修改為4, 輸出12.35, 對最后一位四舍五入// 精度修改后,持續有效,直到精度再次被修改cout.precision(4);cout << value << endl;// 使用定點法, 精度變成小數點后面的位數// 輸出12.3457cout.flags(cout.fixed); cout << value << endl;// 定點法持續有效// 輸出3.1416cout << 3.1415926535 << endl;// 把精度恢復成有效數字位數cout.unsetf(cout.fixed);cout << value << endl; //輸出12.35cout << 3.1415926535 << endl; //輸出3.142system("pause");return 0; }總結
- 上一篇: Qt中的QTimer
- 下一篇: Qt中的QMainWindow