Solved

C++ oftream decimal precision

Posted on 2008-10-15
3
968 Views
Last Modified: 2013-11-05
Having trouble figuring out how to set decimal precision when writing ofstream to .txt file. When I open the text file produced by the following code the result is "0.111111" and not the full number of expected decimal places.


ofstream OF;
OF.open("file_name.txt");
 
double n = 0.1111111111111111;
OF << n << endl;

Open in new window

0
Comment
Question by:Fippy_Darkpaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 105 total points
ID: 22722505
<iomanip> will help you here:


#include <iomanip>
#uncing namespace std;
 
ofstream OF;
OF.open("file_name.txt");
 
size_t prec = 5;
double n = 0.1111111111111111;
OF << setprecision(prec) << n << endl;

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 22722518
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 20 total points
ID: 22722904
>>>> #uncing namespace std;

Maybe

 using namespace std;

is better  ;-)

To add to above comment:

When including iomanip you also could use the ios members fixed and scientific to specify how the floating point was displayed. Note if using fixed the setprecision will determine the number of digits after the decimal point. Otherwise it is the number of significant digits.

   OF << fixed << setprecision(prec) << n << endl;

Note, it maybe dependent on the compiler but I had made the experience that if ommitting the 'fixed' the precision specifies the number of significant digits to display. That is different to that the docs say (and it may have been some time when I lastly tested it).
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c++ how to tell if the progra is ctl or mfc atl ect 6 110
C++ get user from AD  (VS6) 7 111
find Speed using GPS latitude & longitude 4 157
Gaming Software 1 38
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question