Aditya Sihag
Aditya Sihag

Reputation: 5167

c++ boost lexical cast double to string

For the following piece of code

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

i get the following output:

2.3399999999999999

Whereas 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 am looking for the latter's output representation, and not the former.

Thanks,

Upvotes: 4

Views: 1362

Answers (1)

sebm
sebm

Reputation: 11

This has nothing to do with the boost::lexical_cast, but it comes along with double's internal representation:

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

Upvotes: 1

Related Questions