C++ 演练 cout.setf(ios::fixed); 和 cout.precision();

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10181149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:43:21  来源:igfitidea点击:

C++ Walkthrough cout.setf(ios::fixed); and cout.precision();

c++walkthrough

提问by Ali

/* Problem 38 */
        #include <iostream>
        using namespace std;
        class abc {
        double n;
          public:
        abc() { n = 67.5; cout << "1\n"; }
        abc(double num) { set(num); cout << "2\n"; }
        double get() const { cout<<"3\n"; return n; }
        virtual void set(double num) {
            if (num < 10)
            n = 10;
            else if (num > 100)
            n = 100;
            else
            n = num;
            cout << "4\n";
        }
        };
        class def: public abc {
        double m;
          public:
        def() { m = 6.2; cout << "5\n"; }
        def(double num1, double num2): abc(num1) {
            set(num2 - abc::get()); cout << "6\n"; }
        double get() const {
            cout << "7\n"; return m + abc::get(); }
        void set(double num) {
            if (num < 10 || 100 < num)
            m = num;
            else
            m = 55;
            cout << "8\n";
        }
        };
        void do_it(abc &var, double num)
        {   cout << var.get() << '\n';
        var.set(num);
        cout << var.get() << '\n';
        }
        int main()
        {   abc x(45);
        def y(2, 340);
        cout.setf(ios::fixed);
        cout.precision(3);
        do_it(x, 200);
        do_it(y, 253);
        cout << x.get() << '\n';
        cout << y.get() << '\n';
        return 0;
        }

With the above code I just wanted to know what below two lines will really do in the above code

有了上面的代码,我只想知道下面两行在上面的代码中真正会做什么

cout.setf(ios::fixed);cout.precision(3);

cout.setf(ios::fixed);cout.precision(3);

Please do not just give me answer some explanation would be so appreciated because I'm doing a walkthrough to prepare for my final exam tomorrow.

请不要只是给我答案,因为我正在做一个演练,为明天的期末考试做准备。

I searched and some source says it is to set flags but really I don't get what is the concept of it and how it works

我搜索了一些消息来源说这是设置标志但实际上我不明白它的概念是什么以及它是如何工作的

回答by bjhend

cout.setf(ios::fixed)

makes cout print floats with a fixed number of decimals and

使 cout 打印浮点数具有固定的小数位数和

cout.precision(3)

sets this number to be three.

将此数字设置为 3。

For example, if you got a

例如,如果你有一个

double f = 2.5;

then

然后

cout << f;

will print

将打印

2.500

回答by Erwald

Great documentation about how to format your output : Output formatting

关于如何格式化输出的优秀文档:输出格式

It's always usefull when you're trying to do a command-line UI.

当您尝试执行命令行 UI 时,它总是很有用。

回答by Erwald

#include<iostream>

using namespace std;

int main(){
    float large = 2000000000;
    cout << large;
    return 0;
}

The output will be in scientific notation as:

输出将采用科学记数法,如下所示:

2e+009

In order to get the value as it is you should use cout.setf(ios::fixed)

为了获得原样的价值,您应该使用 cout.setf(ios::fixed)

#include<iostream>

    using namespace std;

    int main(){
        cout.setf(ios::fixed);
        float large = 2000000000;
        cout << large;
        return 0;
    }

The output will be as it is with default precision which is 6 for float.

输出将与默认精度相同,浮点数为 6。

2000000000.000000

You can set the precision in the above code as:

您可以将上述代码中的精度设置为:

cout.precision(7);

.....

.....

回答by TheEngineer

It's similar to including the manipulation library:
include :

它类似于包含操作库:
include :

<iomanip>

And then using the following functions

然后使用以下功能

cout << fixed << showpoint;
cout << setprecision(3);