淺學showpoint、setprecision和fixed的程式碼筆記
阿新 • • 發佈:2018-12-17
#include <iostream> #include <iomanip> using namespace std; int main() { double q =15.14259863; cout << showpoint << q << endl; //預設輸出六位有效數字 cout << setprecision(3) << q << endl; //定義輸出有效位數字 cout << showpoint << q << endl; cout << setprecision(7) << q << endl; cout << showpoint << q << endl; return 0; }
#include<iostream> #include<iomanip> using namespace std; int main() { float i; double j; i = 10/3.0; j = 10/3.0; cout << fixed; //讓浮點型固定以數字的方式顯示 cout << setprecision(5); //控制顯示的小數位數 cout << i << " " << endl; cout << setprecision(8); cout << j << " " << endl; return 0; }