#include <iostream> #include <iomanip> using namespace std; int main( void ) { const double value = 12.3456789; cout << value << endl; // 默認以6精度,所以輸出為 12.3457 cout << setPRecision(4) << value << endl; // 改成4精度,所以輸出為12.35 cout << setprecision(8) << value << endl; // 改成8精度,所以輸出為12.345679 cout << fixed << setprecision(4) << value << endl; // 加了fixed意味著是固定點方式顯示,所以這里的精度指的是小數位,輸出為12.3457 cout << value << endl; // fixed和setprecision的作用還在,依然顯示12.3457 cout.unsetf( ios::fixed ); // 去掉了fixed,所以精度恢復成整個數值的有效位數,顯示為12.35 cout << value << endl; cout.precision( 6 ); // 恢復成原來的樣子,輸出為12.3457 cout << value << endl; }
新聞熱點
疑難解答