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

不想你离开。 提交于 2019-12-08 07:08:30

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!