Jump to content
  • Advertisement
Sign in to follow this  
random_thinker

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by random_thinker
But, 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Fruny
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.


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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!