Prevent scientific notation in ostream when using

2019-01-03 06:42发布

I need to prevent my double to print in scientific notation in my file,

when I do this

outfile << X;

4条回答
太酷不给撩
2楼-- · 2019-01-03 07:24

To set formatting of floating variables you can use a combination of setprecision(n), showpoint and fixed. In order to use parameterized stream manipulators like setprecision(n) you will have to include the iomanip library:

#include <iomanip>

setprecision(n): will constrain the floating-output to n places, and once you set it, it is set until you explicitly unset it for the remainder of the stream output.

fixed: will enforce that all floating-point numbers are output the same way. So if your precision is set to 4 places, 6.2, and 6.20 will both be output as:

6.2000
6.2000

showpoint: will force the decimal portions of a floating-point variable to be displayed, even if it is not explicitly set. For instance, 4 will be output as:

4.0

Using them all together:

outfile << fixed << showpoint;
outfile << setprecision(4);
outfile << x;
查看更多
再贱就再见
3楼-- · 2019-01-03 07:28
迷人小祖宗
4楼-- · 2019-01-03 07:36

Here's an example of usage http://cplus.about.com/od/learning1/ss/clessontwo_4.htm

as per your question use

  cout<< fixed<< a<< endl;
查看更多
Emotional °昔
5楼-- · 2019-01-03 07:37

All the above answers were useful, but none directly answer the question.

outfile.setf(ios_base::fixed);
outfile << x;

I found the answer in @moogs link: http://www.cplusplus.com/reference/iostream/ios_base/fmtflags/

Here's a demo program: http://ideone.com/FMxRp1

查看更多
登录 后发表回答