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

setting precision for stringstream

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

Hi, How can I set the number of digits after the decimal point in the following example? std::string str; std::stringstream stream; float floatVal=12.012345675f stream << floatal; Thanks

Share this post

Link to post
Share on other sites
streamsize ios_base:: precision() const;
- Returns the precision.
streamsize ios_base:: precision(streamsize prec);
- Sets the precision, returns the previously used precision.
stream << floatVal;

or use the std::setprecision(int n) manipulator.
stream << std::setprecision(5) << floatVal;  

Or just use boost::format - it's much easier
boost::format fmt( "%.5f" );
stream << fmt % floatVal;

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on October 10, 2003 4:19:08 AM]

Share this post

Link to post
Share on other sites