C++ 使用 cout 以精度 4 打印双精度

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

print double with precision 4 using cout

c++doubleprecisioncout

提问by URL87

Possible Duplicate:
Convert a double to fixed decimal point in C++

可能的重复:
在 C++ 中将 double 转换为固定小数点

Suppose , I have double a = 0and I want to print it as 0.0000.

假设,我有double a = 0并且我想将其打印为0.0000.

I've tried this :

我试过这个:

cout.precision(4) ; 
cout<<a<<endl ; 

but it gaves 0as the output.

但它0作为输出给出。

回答by SeMeKh

Just try:

你试一试:

#include <iomanip>
...
cout << fixed << setprecision(4);
cout << a << endl;

See here.

这里

回答by crh225

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


int main()
{
double a = 0.00;
// print a double, 2 places of precision 
cout << setprecision(4) << a << endl;
}