C++ =》输出流小数点后位数控制

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    float a = 10.5;
    /*库函数:iomanip
        函数:
        setiosflags(ios::fixed);->将流的格式设置为:fixed(将一个浮点数表示为一个定点整数和小数点和小数部分的格式)。
        setiosflags(ios::showpoint);->显示小数点
        setprecision(2);->精度
        setw(10);->域宽
        right; ->居右显示
    */
    cout<<setiosflags(ios::fixed)<<setw(10)<<right<<setiosflags(ios::showpoint)<<setprecision(2)<<a;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40722661/article/details/80054542