setting precision for stringstream

Started by
2 comments, last by Floating 20 years, 6 months ago
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
Advertisement
streamsize ios_base:: precision() const;
- Returns the precision.
streamsize ios_base:: precision(streamsize prec);
- Sets the precision, returns the previously used precision.
stream.precision(5);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]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thanks Fruny!

What header is needed for "setprecision"?

I searched the stl but it seems it is not part of it?
IIRC, it''s in <iomanip>
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement