输出浮点数为三位或更多,以避免指数

Output float as three digits, or more to avoid exponent

本文关键字:指数 浮点数 位或 输出      更新时间:2023-10-16

我正在尝试将浮点数输出为三位数字,或者在必要时输出更多以避免指数。

一些例子:

0.12 // 0.123 would be ok
1.23
12.3
123
1234
12345

我得到的最接近的是

std::cout << std::setprecision(3) << f << std::cout;

但这打印了类似的东西

21       // rather than 21.0
1.23e+03 // rather than 1234

std::setprecisionstd::fixed相结合意味着我总是得到相同数量的十进制后数字,这不是我想要的。

使用std::setw,123.456仍将打印为123.456而不是123。

有什么建议吗?

据我所知,解决此问题的最简单方法是滚动一个函数来捕获它。 我把它放在一起,它似乎有效。 我不确定您是否希望大数字只有 3 位有效数字,或者它们是否应该将所有 sig 无花果保留在小数点后的左侧,但进行这种修改并不难:

void printDigits(float value, int numDigits = 3)
{
    int log10ofValue = static_cast<int>(std::log10(std::abs(value)));
    if(log10ofValue >= 0) //positive log means >= 1
    {
        ++log10ofValue;  //add 1 because we're culling to the left of the decimal now
        //The difference between numDigits and the log10 will let us transition across the decimal
        // in cases like 12.345 or 1.23456 but cap it at 0 for ones greater than 10 ^ numDigits
        std::cout << std::setprecision(std::max(numDigits - log10ofValue, 0)); 
    }
    else
    {
        //We know log10ofValue is <= 0, so set the precision to numDigits + the abs of that value
        std::cout << std::setprecision(numDigits + std::abs(log10ofValue));
    }
    //This is a floating point truncate -- multiply up into integer space, use floor, then divide back down
    float truncated = std::floor(value * std::pow(10.0, numDigits - log10ofValue)) / std::pow(10.0, numDigits - log10ofValue);
    std::cout << std::fixed << truncated << std::endl;
}

测试:

int main(void)
{
    printDigits(0.0000000012345);
    printDigits(12345);
    printDigits(1.234);
    printDigits(12.345678);
    printDigits(0.00012345);
    printDigits(123456789);
    return 0;
}

输出:

0.00000000123
12300
1.23
12.3
0.000123
123000000

这是我提出的解决方案。丑陋,但我相信它有效。

if(f>=100) {
    std::cout << std::fixed << std::setprecision(0) << f << std::endl;
    std::cout.unsetf(std::ios_base::floatfield);
} else {
    std::cout << std::showpoint << std::setprecision(3) << f << std::noshowpoint << std::endl;
}

如果有人知道如何简化这一点,请告诉我!