1. 程式人生 > >C++輸出保留有效數字

C++輸出保留有效數字

class 小數點 main span lag res end turn TP

#include <iomanip>  
//保留小數點後2位
cout << setiosflags(ios::fixed) << setprecision(2) << val << endl;
//保留2位有效數字
cout << setprecision(2) << val << endl;
#include <iomanip>  
using namespace std;

int main()
{
    double val = 10.6;
    
    cout << setprecision(2
) << val << endl;//保留兩位小數 cout << setiosflags(ios::fixed) << setprecision(2) << val << endl;//保留小數點後兩位 cout << resetiosflags(ios::fixed) << setprecision(2) << val << endl;//清除ios::fixed system("pause"); return 0; }

技術分享圖片

C++輸出保留有效數字