c++保留小数后几位

头文件#include<iomanip>

1.需要补零

cout.setf(ios::fixed);

cout<<fixed<<setprecision(需要精确到小数点后几位)<<ans<<endl;

2.不需补零

(1)末尾零被省略

cout<<setprecision(需要精确到小数点后几位)<<ans<<endl;

(2)取消设置fixed

cout.unsetf(ios::fixed);

cout<<setprecision(需要精确到小数点后几位)<<ans<<endl;

posted @ 2019-12-02 21:08  Tong|Chen  阅读(4625)  评论(0)    收藏  举报