Archived

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

jeeky

stream formatted output

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