简体   繁体   中英

Convert double to string with fixed point notation, no trailing zeroes and witout sprintf

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 ?

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM