C++入门经典-例2.10-控制输出精确度

1:代码如下:

// 2.10.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
void main()
{
    int x=123;
    double y=-3.1415;
    cout << "x=";
    cout.width(10);//输出字符串的宽度为10
    cout << x;
    cout << "y=";
    cout.width(10);//输出字符串的宽度为10
    cout << y <<endl;
    cout.setf(ios::left);//输出数据在本域宽范围内向左对齐
    cout << "x=";
    cout.width(10);//输出字符串的宽度为10
    cout << x;
    cout << "y=";
    cout << y <<endl;
    cout.fill('*');//在空余处用*填充
    cout.precision(4);//取4位有效数字
    cout.setf(ios::showpos);//强制显示符号
    cout << "x=";
    cout.width(10);//输出宽度设为10
    cout << x;
    cout << "y=";
    cout.width(10);//输出宽度设为10
    cout << y <<endl;
}
View Code

运行结果:

原文地址:https://www.cnblogs.com/lovemi93/p/7505395.html