输出浮点数为三位或更多,以避免指数
Output float as three digits, or more to avoid exponent
我正在尝试将浮点数输出为三位数字,或者在必要时输出更多以避免指数。
一些例子:
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::setprecision
与std::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;
}
如果有人知道如何简化这一点,请告诉我!
相关文章:
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 使用提升将数据从 PyObject 复制到浮点数 *
- 使用浮点数和双精度数的非常小数字的数学
- 使用英特尔内联函数将打包的 8 位整数乘以浮点数向量
- 如何在 c++ 中将小数点后两位数的浮点数分配给另一个浮点数
- 返回浮点数的小数位数
- txt 文件中浮点数的最大和最小值
- 为什么 std::cout 打印浮点数、双精度和长双精度到相同的小数精度?
- 将浮点数转换为无符号字符数组并打印出来
- 如何将时间字符串 (M:SS) 转换为浮点数
- 如何将浮点数(*)[6]转换为浮点**类型?
- C++将浮点数乘以分数
- 如何将浮点数转换为uint8_t?
- 在 C/C++ 中以指数形式将浮点数声明为宏的正确方法是什么?
- 输出浮点数为三位或更多,以避免指数
- 如何在 c++ 下对尾数和双精度或浮点数的指数部分进行操作(快速)
- 在gcc中使用printf控制浮点数的指数位数
- 从符号尾数和指数构造浮点数