#include<bits/stdc++.h>
using namespace std;
int main()
{ double x=123.45;
cout.precision(1);
cout<<fixed<<x<<" ";
return 0;
}
運行結果是:123.5
#include<bits/stdc++.h>
using namespace std;
int main()
{ double x=123.455;
cout.precision(2);
cout<<fixed<<x<<" ";
return 0;
}
運行結果是:123.45(沒有四捨五入)
#include<bits/stdc++.h>
using namespace std;
int main()
{ double x=123.4555;
cout.precision(3);
cout<<fixed<<x<<" ";
return 0;
}
運行結果是:123.456
保留一位小數、三位小數都進行了四捨五入,恰恰是保留兩位小數的時候,沒有進行四捨五入。