如何在 C++ 中使用 setprecision

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22515592/
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-28 00:06:35  来源:igfitidea点击:

How to use setprecision in C++

c++

提问by Malik

I am new in C++, i just want to output my point number up to 2 digits. just like if number is 3.444, then the output should be 3.44or if number is 99999.4234then output should be 99999.42, How can i do that. the value is dynamic. Here is my code.

我是新来的C++,我只想输出最多 2 位的点数。就像如果数字是3.444,那么输出应该是3.44或者如果数字是99999.4234那么输出应该是99999.42,我该怎么做。该值是动态的。这是我的代码。

#include <iomanip.h>
#include <iomanip>
int main()
{
    double num1 = 3.12345678;
    cout << fixed << showpoint;
    cout << setprecision(2);
    cout << num1 << endl;
}

but its giving me an error, undefined fixed symbol.

但它给了我一个错误,未定义的固定符号。

回答by piokuc

#include <iomanip>
#include <iostream>

int main()
{
    double num1 = 3.12345678;
    std::cout << std::fixed << std::showpoint;
    std::cout << std::setprecision(2);
    std::cout << num1 << std::endl;
    return 0;
}

回答by Loyce

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

You can enter the line using namespace std;for your convenience. Otherwise, you'll have to explicitly add std::every time you wish to use cout, fixed, showpoint, setprecision(2)and endl

using namespace std;为方便起见,您可以输入该行。否则,你必须明确地添加std::每次你想使用时间coutfixedshowpointsetprecision(2)endl

int main()
{
    double num1 = 3.12345678;
    cout << fixed << showpoint;
    cout << setprecision(2);
    cout << num1 << endl;
return 0;
}

回答by aliasm2k

The answer above is absolutely correct. Here is a Turbo C++ version of it.

上面的答案是完全正确的。这是它的 Turbo C++ 版本。

#include <iomanip.h>
#include <iostream.h>

void main()
{
    double num1 = 3.12345678;
    cout << setiosflags(fixed) << setiosflags(showpoint);
    cout << setprecision(2);
    cout << num1 << endl;
}

For fixedand showpoint, I think the setiosflagsfunction should be used.

对于fixedand showpoint,我认为setiosflags应该使用该函数。

回答by aliasm2k

Replace These Headers

替换这些标题

#include <iomanip.h>
#include <iomanip>

With These.

用这些。

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

Thats it...!!!

就是这样...!!!

回答by user13461811

    #include <bits/stdc++.h>                        // to include all libraries 
using namespace std; 
int main() 
{ 
double a,b;
cin>>a>>b;
double x=a/b;                                 //say we want to divide a/b                                 
cout<<fixed<<setprecision(10)<<x;             //for precision upto 10 digit 
return 0; 
} 

/*input : 1987 31 output : 662.3333333333 10 digits after decimal point */

/*输入:1987 31 输出:662.3333333333 小数点后 10 位 */

回答by Pankaj Kumar Thapa

Below code runs correctly.

下面的代码运行正确。

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

int main()
{
    double num1 = 3.12345678;
    cout << fixed << showpoint;
    cout << setprecision(2);
    cout << num1 << endl;
}