控制cout的輸出格式
1、顯示精度:
'''c++
//強制以小數方式顯示
cout<< fixed;
//控制顯示的精度
cout<< setprecision(2); //小數後面輸出兩個數字。需要頭文件 #include<iomanip>
//使用:
cout<<fixed << setprecision(2);//後面的所有cout輸出都是這中格式
cout<<3.1415926<<endl;
'''
2、輸出寬度設置
'''c++
cout<<setw(8); //輸出的整體寬度爲8,僅對後面緊挨着的一個輸出管用
//使用:
cout<< setw(8) << 3.14<< 1.414 <<endl; //setw(8)僅對3.14有用,使3.14佔8個寬度,默認右對齊
'''
3、左右對齊:
'''c++
cout<< left; //左對齊
cout<< right; //右對齊,默認爲右對齊
//使用:
int num = 10;
cout << left;
cout << setw(8) << num << endl; ///輸出的10佔8位,且左對齊
'''
4、設置不足位數填充的字符:
'''c++
cout << setfill('#'); //設置不足的位數用‘#’號填充
//使用:
int num = 10;
cout << setfill('#');
cout << setw(8) << num << endl; //輸出num, 共佔8位,不足8位用‘#’號填充
'''