C ++ boost lexical cast double to string

For the next code snippet

std::cout<<boost::lexical_cast<std::string>(2.34)<<std::endl 

I get the following output:

 2.3399999999999999 

If i do

  double d = 2.34; std::stringstream ss; ss<<d; std::string s = ss.str(); cout<<s<<endl; 

I get the following output:

  2.34 

Why is this happening? Obviously, I'm looking for the last output presentation, not the first.

Thanks,

+4
source share
1 answer

This has nothing to do with boost :: lexical_cast, but it happens along with a double internal representation:

See also this answer: C ++ internal representation double / float

+1
source

All Articles