double val = 0.1;
std::stringstream ss;
ss << val;
std::string strVal= ss.str();
In the Visual Studio debugger, val
has the value 0.10000000000000001 (because 0.1 can't be represented).
When val
is converted using stringstream, strVal
is equal to "0.1"
. However, when using boost::lexical_cast, the resulting strVal
is "0.10000000000000001"
.
Another example is the following:
double val = 12.12305000012;
Under visual studio val
appears as 12.123050000119999, and using stringstream and default precision (6) it becomes 12.1231. I don't really understand why it is not 12.12305(...).
Is there a default precision, or does stringstream have a particular algorithm to convert a double value which can't be exactly represented?
Thanks.