發現一個c++保留2位小數的BUG

#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

保留一位小數、三位小數都進行了四捨五入,恰恰是保留兩位小數的時候,沒有進行四捨五入。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章