Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the precision for stringstream globally

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.

like image 807
rkb Avatar asked Mar 04 '10 20:03

rkb


1 Answers

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.

like image 138
Patrick Johnmeyer Avatar answered Sep 29 '22 01:09

Patrick Johnmeyer