Today I tried out some new functions of the C++11 STL and encountered std::to_string
.
Lovely, lovely set of functions. Creating a stringstream object for just one double-to-string conversion always seemed overkill to me, so I'm glad we can now do something like this:
std::cout << std::to_string(0.33) << std::endl;
The result?
0.330000
I'm not entirely content with that. Is there a way to tell std::to_string
to leave out the trailing zeros? I searched the internet, but as far as I can see the function takes only one argument (the value to be converted). Back in 'the old days' with stringstreams, you could set the width of the stream, but I'd rather not convert back.
Anyone encountered this problem before/has a solution? Some StackOverflow searches yielded nothing.
(A C++11 STL reference: http://en.cppreference.com/w/cpp/string/basic_string/to_string)
See Question&Answers more detail:os