double类型控制输出宽度

    科技2022-07-14  120

    double类型控制输出宽度产生的疑惑

    代码如下:

    #include <iostream> #include <iomanip> using namespace std; int main() { int a = 1234567890; //double b = 12.159756; double b = 12.35; float c = 3.14159F; cout << "输出宽度为10:" << endl << setw(10) << a << endl; cout << "输出宽度为10:" << endl << setw(10) << b << endl; cout << "输出宽度为10:" << endl << setw(10) << c << endl; cout << "输出宽度为8:" << endl << setw(8) << a << endl; cout << "输出宽度为8:" << endl << setw(8) << b << endl; cout << "输出宽度为8:" << endl << setw(8) << c << endl; cout << "输出宽度为6:" << endl << setw(6) << a << endl; cout << "输出宽度为6:" << endl << setw(6) << b << endl; cout << "输出宽度为6:" << endl << setw(6) << c << endl; cout << "输出宽度为4:" << endl << setw(4) << a << endl; cout << "输出宽度为4:" << endl << setw(4) << b << endl; cout << "输出宽度为4:" << endl << setw(4) << c << endl; return 0; }

    输出: /* 输出宽度为10: 1234567890 输出宽度为10: 12.1598 输出宽度为10: 3.14159 输出宽度为8: 1234567890 输出宽度为8: 12.1598 输出宽度为8: 3.14159 输出宽度为6: 1234567890 输出宽度为6: 12.1598 输出宽度为6: 3.14159 输出宽度为4: 1234567890 输出宽度为4: 12.1598 输出宽度为4: 3.14159

    另外测试了一组数据: 输出宽度为10: 1234567890 输出宽度为10: 12.35 输出宽度为10: 3.14159 输出宽度为8: 1234567890 输出宽度为8: 12.35 输出宽度为8: 3.14159 输出宽度为6: 1234567890 输出宽度为6: 12.35 输出宽度为6: 3.14159 输出宽度为4: 1234567890 输出宽度为4: 12.35 输出宽度为4: 3.14159

    观察输出,发现如果要输出的数值达不到setw设置的宽度时,会在前面补空格,且右对齐; 如果要输出的数值超过setw设置的宽度时,又会全部输出; 但是我发现double类型就很奇怪,为何明明宽度足够,却四舍五入截断了?

    为了清楚一点 我把空格填为*,setw默认填空格 代码:

    /* 程序功能:控制输出宽度 修改版 把空格填充* @author lymilicode @version 1.1.201004 @参数说明: */ #include <iostream> #include <iomanip> using namespace std; int main() { int a = 1234567890; double b = 12.159756; //double b = 12.35; float c = 3.14159F; cout << "输出宽度为10:" << endl << setfill('*') << setw(10) << a << endl; cout << "输出宽度为10:" << endl << setfill('*') << setw(10) << b << endl; cout << "输出宽度为10:" << endl << setfill('*') << setw(10) << c << endl; cout << "输出宽度为8:" << endl << setfill('*') << setw(8) << a << endl; cout << "输出宽度为8:" << endl << setfill('*') << setw(8) << b << endl; cout << "输出宽度为8:" << endl << setfill('*') << setw(8) << c << endl; cout << "输出宽度为6:" << endl << setfill('*') << setw(6) << a << endl; cout << "输出宽度为6:" << endl << setfill('*') << setw(6) << b << endl; cout << "输出宽度为6:" << endl << setfill('*') << setw(6) << c << endl; cout << "输出宽度为4:" << endl << setfill('*') << setw(4) << a << endl; cout << "输出宽度为4:" << endl << setfill('*') << setw(4) << b << endl; cout << "输出宽度为4:" << endl << setfill('*') << setw(4) << c << endl; return 0; }

    输出: double b=12.159756 double b=12.159755999 999 999 79

    我又测试了几组数据:

    /* 程序功能:控制输出宽度 修改版 把空格填充* @author lymilicode @version 1.1.201004 @参数说明: */ #include <iostream> #include <iomanip> using namespace std; int main() { int a = 1234567890; double b = 12.159756; //double b = 12.35; //float c = 3.14159F; float c = 3.1234567F;//新数据 cout << "输出宽度为10:" << endl << setfill('*') << setw(10) << a << endl; cout << "输出宽度为10:" << endl << setfill('*') << setw(10) << b << endl; cout << "输出宽度为10:" << endl << setfill('*') << setw(10) << c << endl; cout << "输出宽度为8:" << endl << setfill('*') << setw(8) << a << endl; cout << "输出宽度为8:" << endl << setfill('*') << setw(8) << b << endl; cout << "输出宽度为8:" << endl << setfill('*') << setw(8) << c << endl; cout << "输出宽度为6:" << endl << setfill('*') << setw(6) << a << endl; cout << "输出宽度为6:" << endl << setfill('*') << setw(6) << b << endl; cout << "输出宽度为6:" << endl << setfill('*') << setw(6) << c << endl; cout << "输出宽度为4:" << endl << setfill('*') << setw(4) << a << endl; cout << "输出宽度为4:" << endl << setfill('*') << setw(4) << b << endl; cout << "输出宽度为4:" << endl << setfill('*') << setw(4) << c << endl; return 0; }

    输出:

    我发现,无论时double还是float,都只保留6位有效位。有点迷惑要解开的感觉了。 查找C++ primer plus 第十七章,终于让我发现了问题。 教材反复强调了一句话:显示的数字位数与数字被存储时精度没有任何关系。新式浮点数类型被显示位6位,老式浮点类型显示为带6位小数。字段的宽度恰好容纳数字和负号。(如果有的话)

    至此终于解决了我的疑惑。

    Processed: 0.012, SQL: 8