# C++ : **Solved** Best way to set number of decimal places on ostreams

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

What is the best way to set the printed precision of any number type, i.e. int anInt = -2; int anotherInt = 10; float aFloat = -2.0678; double aDouble = 5.91; double anotherDouble = 1.000657843; For reported output, we want, say, 4 decimal places shown for all so that the numbers are printed as: -2.0000 10.0000 -2.0678 5.9100 1.0007 // round-off? --random [Edited by - random_thinker on August 24, 2005 3:41:36 PM]

##### Share on other sites
So, if we have:

ostream out;

then, using Method 1:

out.precision(4);

out << anInt; // prints -2
out << anotherInt; // prints 10
out << aFloat; // prints -2.0678
out << aDouble; // prints 5.9100
out << anotherDouble; // prints 1.0007

but using Method 2:

out << setprecision(4) << anInt; // prints -2.0000
out << setprecision(4) << anotherInt; // prints 10.0000
out << setprecision(4) << aFloat; // prints -2.0678
out << setprecision(4) << aDouble; // prints 5.9100
out << setprecision(4) << anotherDouble; // prints 1.0007

But, isn't it neccesary to reset the ostream with Method 2, or risk all subsequent output as precision(4)?

--random

##### Share on other sites
Quote:
 Original post by random_thinkerBut, isn't it neccesary to reset the ostream with Method 2, or risk all subsequent output as precision(4)?

What do the docs say?

(Yes, that's an attempt to impart a "higher" lesson.)

##### Share on other sites
In my experience the docs on this issue are really confusing. Here's what I did to make it print 4 decimal places all the time:
stream.setf(ios::fixed, ios::floatfield);stream.precision(4);

This will apply for all further output on this stream. I think that when you're using IO manipulators from the header <iomanip> then stuff always applies only for the next output.

##### Share on other sites
From Josuttis' book, I know that .precision() and << setprecision(4) can be used, but he does not indicate the advantages or when one is to be used over the other. Stroustrup is a little better, indicating that:

out << setprecision(n) just calls out.precision(n) on the object whereas the form out.precision(n) affects all output until the next call of out.precision(n). However, it appears that both of these only affect floats and not integers. So how does one set all numbers, including integers to a fixed value? Is Tron3K's method the best? i.e

stream.setf(ios::fixed, ios::floatfield);

--random

##### Share on other sites
You see, when printing, I want to be able to translate a string as either an integer or float (this is no problem, I have excellent routines for this) and then to print a specific precision, where 0 would be for inline integer output (like page numbers, etc.) and n > 0 would fix precision, even for integers, output by output. Both Josuttis' and Stroustup's books, which I have, are not very clear on this.

--random

##### Share on other sites
Precision only concern floating-point values.

If what you are looking for is for 10 to be printed as 00010, you'll be wanting setfill and setw. The width fill must be reset after each stream operation that uses it.

##### Share on other sites
Quote:
 Original post by FrunyPrecision only concern floating-point values.If what you are looking for is for 10 to be printed as 00010, you'll be wanting setfill and setw. The width fill must be reset after each stream operation that uses it.

What I'm considering is this, for an integer value of 10 in columnar form, I may want to print as 10.000. But where 10 is used inline, say in text as a reference to a figure or a page number, I may want to print as 10. What I would like to do is to control this output.

What you are indicating is that I will have to convert the int to float as say atof(10) or lexical_cast<float>(10) before applying the precision method, yes?

--random

##### Share on other sites
Here is the relevent code, that checks for integer or number (float) characters, then sets the precision accordingly:

void Echo::echo(const string & strg,		const string & width,		const string & justification,		const string & precision,		ostream & out){   int w = lexical_cast<int>(width),       p = lexical_cast<int>(precision);   out.flags();      // Reset all flags.   out.width(w);     // Set default width   out.precision(p); // Set default precision.      // Set default justification.   out.setf(ios_base::left,ios_base::adjustfield);   // Reset justification if needed.   if ( justification == "R" || justification == "r" )   {      out.setf(ios_base::right,ios_base::adjustfield);   }   // Output formatted result.   if (isInteger(strg))   {      if ( p > 0)      {	 //Stream as float.	 out.setf(ios::fixed, ios::floatfield);	 out << lexical_cast<float>(strg);      }            else      {	 // Stream as integer.	 out << lexical_cast<int>(strg);      }   }      else if (isNumber(strg))   {      // Stream as float.      out.setf(ios::fixed, ios::floatfield);      out << lexical_cast<float>(strg);   }      else   {      // Stream as string.      out << strg;   }   // Reset formats.   clearFormats();}

Comments would be helpful, it does not appear to be working altoghther. The width setting is correct, the justification is correct and the output is correct, but the precision control is not correct.

--random

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633300
• Total Posts
3011267
• ### Who's Online (See full list)

There are no registered users currently online

×