0

At first I used boost's lexical_cast to do this. But due to the way C++ represents doubles/floats, when I converted 5.1 to a string it would give me 5.0999999 or something to that extent. So, I converted it this way:

stringstream ss;
ss << 3.14159265359;
cout << ss.str();

But this would only give me 3.14159, and I would like more precision than that. I think I saw something about printf() being able to do this, but I am actually working on a Windows GUI program, not outputting to a console. How can I get more than 5 decimal digits of precision? I am willing to settle for 8, but 10 or 11 would be nice. Is this too much to ask for given how C++ represents floats and doubles?

4

1 回答 1

2
ss << setprecision(12) << 3.14159265359;

Also, the choice between using streams and the C printf/scanf family has nothing to do with GUI vs. console. The C equivalent of ostringstream is sprintf.

于 2012-10-04T14:12:39.763 回答