Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


stream formatted output

This topic is 5939 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

Can anybody tell me how to produce formatted output using streams instead of printf? Specifically, I am looking for a way to only show a desired number of significant digits when printing numbers. Example: foo = 5.0f; printf("%2.2f %2.5f\n", foo, foo); produces: 5.00 5.00000 When using a stream, you do this: foo = 5.0f; cout << foo << " " << foo << endl; The output is: 5.00000 5.00000 Unfortunately, I don't know how to trim the significant digits and I haven't found anything in texts or on the web. Can anybody answer this question? Thanks, jeeky [edited by - jeeky on July 15, 2002 10:03:21 AM] [edited by - jeeky on July 15, 2002 10:03:55 AM]

Share this post

Link to post
Share on other sites
Check out the stream manipulators. Specifically, setprecision() should do what you want. For example:

cout << setprecision(5) << 0.123456789 << endl;

Your compiler help should list all the available manipulators.

Share this post

Link to post
Share on other sites

  • 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!