I am using stringstream in my entire project which has more than 30 files. I recently overcomed an issue caused by stringstring where I was parsing the double to stringstream and there was a precision lost. So now I want to set the precision for all the files. Is there any way to set it somewhere globally so that I dont need to make changes everywhere going into each file. Someone suggested me to see if its possible using locale.
Please help me out with the issue and if you have code or any link to code, it will be more useful.
Probably the easiest way to do this is to replace your use of stringstream throughout your program with your own class that inherits from stringstream
:
class mystringstream : public std::stringstream
{
public:
mystringstream()
{
precision(16); // or whatever your desired precision is
}
};
The precision
method is defined way up the inheritance chain in std::ios_base
, and controls the number of significant digits, or the number of digits after the decimal if the fixed
manipulator is in play.
For more example code and output see this paste on codepad.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With