print double with precision 4 using cout [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-05 17:00:33

问题


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

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

I've tried this :

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

but it gaves 0 as the output.


回答1:


Just try:

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

See here.




回答2:


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


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


来源:https://stackoverflow.com/questions/13728425/print-double-with-precision-4-using-cout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!