setprecision(n) //設顯示小數精度為n位 包含整數位
#include<iostream>
#include<iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
float pi = 3.1415926;
float fnum = 123.456789;
cout << setprecision(3) << pi << endl;
cout << setprecision(3) << fnum << endl;
cout << setprecision(4) << fnum << endl;
cout << setprecision(2) << fnum << endl;
return 0;
}
// 輸出
3.14
123
123.5
1.2e+002
百度百科給出的例子 我自己豐富了一下
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
// precision
// n. 精密, 精確度, 精確
// fixed
// adj. 固定的; 不變的; 確定的; 不動的
cout << 12345.0 << endl;
//輸出12345
//cout<<fixed<<setprecision(2)<<123.456<<endl;
/*如果在這個位置就加上fixed的話,后面的輸出全部都按照fixed處理*/
cout << setprecision(4) << 3.1415926 << endl;
//輸出的結果是3.142
cout << setprecision(3) << 12345.0 << endl;
//輸出的結果是 "1.23e+004 "
cout << fixed << setprecision(2) << 123.456 << endl;
//前面有fixed 設置的是小數的精度 輸出的結果是123.46,要進行四舍五入
cout << fixed << setprecision(1) << 123.345 << endl;
//輸出的結果是123.3,要進行四舍
cout << setprecision(4) << 123.456 << endl;
//輸出的結果是123.4560,補位 這里沒有輸入fixed 也是按前面的標準來的
cout << showpoint << 12345.0 << endl;
//輸出12345.00 如果輸出位出現0
cout << showpoint << 12345 << endl;
//輸出12345 showpoint有什么用 下一講研究
}
小記到此