?
Solved

C++ oftream decimal precision

Posted on 2008-10-15
3
Medium Priority
?
972 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 420 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 80 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

800 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