This question has been asked a couple of times, but all the answers either refer to sprintf or involve deleting the trailing zeroes manually. Is there really no better way? is it not possible to achieve this with std::stringstream
?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- how to split a list into a given number of sub-lis
- thread_local variables initialization
相关文章
- JSP String formatting Truncate
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
If the goal is to have a fixed point of
x
or less for trailing zeros, toss the double through a fixed sstream. Then cut off the remaining with non fixed output.Edit: To save results
First you calculate how many potential digits you have before and after the decimal:
Then you find out how many of those digits are zeros:
Now you have a value you can use with
std::setprecision
:Ultimately this is a lot of work and duplicates effort that the string conversion does anyway, which is why people generally just convert to a string and remove the trailing zeros. And no, there's no simple formatting option to do this, you have to do it the hard way or not at all.
See the above code in action: http://ideone.com/HAk55Y