-->

Convert double to string with fixed point notation

2019-06-02 11:08发布

问题:

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?

回答1:

First you calculate how many potential digits you have before and after the decimal:

int digits_before = 1 + (int)floor(log10(fabs(value)));
int digits_after = std::numeric_limits<double>::digits10 - digits_before;

Then you find out how many of those digits are zeros:

double whole = floor(pow(10, digits_after) * fabs(value) + 0.5);
while (digits_after > 0 && (whole/10.0 - floor(whole/10.0)) < 0.05)
{
    --digits_after;
    whole = floor(whole / 10.0 + 0.5);
}
if (digits_after < 0) digits_after = 0;

Now you have a value you can use with std::setprecision:

std::stringstream ss;
ss << std::fixed << std::setprecision(digits_after) << value;

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



回答2:

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.

double number = 1.03000;
std::stringstream s.precision(6);

s << number;
std::cout << s.str();

Edit: To save results

std::string result = s.str();