std::string
如果可能的话;如果不是,字符串应采用最小的数字,则优先使用“ 100”而不是“ 1E2”或“ 1000”或1E03“或“ 0.01”至“ 1E-2”,等等。
可以通过std ::格式(或任何其他类型的std格式)来实现这一点,而无需手动转换float的每个数字?
fit_to
.
以下版本的
#include <iostream>
std::string to_string_shortest(float v, [[maybe_unused]] std::size_t fit_to = 4) {
return std::format("{}", v);
}
int main()
{
std::cout << to_string_shortest(3.1415926f) << '\n'; // Want to have 3.14
std::cout << to_string_shortest(1e7f) << '\n'; // Want to have 1e+7 (1e7 acceptable)
std::cout << to_string_shortest(1e-7f) << '\n'; // Want to have 1e-7
std::cout << to_string_shortest(1.2345e20f) << '\n'; // Want to have 1e20
std::cout << to_string_shortest(1.2345e-20f) << '\n'; // Want to have 1e-20
std::cout << to_string_shortest(3.1415926f, 8) << '\n'; // Want to have 3.141593
std::cout << to_string_shortest(-3.1415926f, 8) << '\n'; // Want to have -3.14159
std::cout << to_string_shortest(1.2345e20f, 8) << '\n'; // Want to have 1.234e20
std::cout << to_string_shortest(1.2345e-20f, 8) << '\n'; // Want to have 1.23e-20
std::cout << to_string_shortest(0.001f) << '\n'; // Want to keep as is
std::cout << to_string_shortest(1.0f) << '\n'; // Want to keep as is
std::cout << to_string_shortest(1e20f) << '\n'; // Want to keep as is
std::cout << to_string_shortest(1e-20f) << '\n'; // Want to keep as is
}
可能不是完美的,但它很近。主要是,在某些指数值上有落后空间。输出周围有
to_string_shortest
显示整个场宽度。在Compilerexplorer.
[]
到
-e0
和修剪的尾随空间的更改指数。
-e
输出为:
auto to_string_shortest(float num, std::size_t fit_to = 4) -> std::string {
// get digits in num
int width = (num == 0) ? 1 : (std::log10(std::abs(num)) + 1);
// limit width - keep > 0
width = std::min(std::abs(width), 10);
// '<' left justifies the output
std::string fmt = std::format("{:<{}.{}}", num, width, fit_to);
// replace -e0 with just -e
auto exponent_position{fmt.rfind("e-0")};
if (exponent_position != std::string::npos) {
fmt.replace(exponent_position, 3, "-e");
}
// trim trailing white space
auto pos = fmt.find_last_of(' ');
while (pos != std::string::npos) {
fmt.erase(pos);
pos = fmt.find_last_of(' ');
}
return "[" + fmt + "]"; // "[ ]]" just to show no spaces
}
int main() {
std::cout << to_string_shortest(3.1415926f) << '\n'; // Want to have 3.14
std::cout << to_string_shortest(1e7f) << '\n'; // Want to have 1e+7 (1e7 acceptable)
std::cout << to_string_shortest(3.1415926f, 8) << '\n'; // Want to have 3.141593
std::cout << to_string_shortest(-3.1415926f, 8) << '\n'; // Want to have -3.14159
std::cout << to_string_shortest(0.001f) << '\n'; // Want to keep as is
std::cout << to_string_shortest(1.0f) << '\n'; // Want to keep as is
std::cout << to_string_shortest(1e-7f) << '\n'; // Want to have 1e-7
std::cout << to_string_shortest(1.2345e20f) << '\n'; // Want to have 1e20
std::cout << to_string_shortest(1.2345e-20f) << '\n'; // Want to have 1e-20
std::cout << to_string_shortest(1.2345e20f, 8) << '\n'; // Want to have 1.234e20
std::cout << to_string_shortest(1.2345e-20f, 8) << '\n'; // Want to have 1.23e-20
std::cout << to_string_shortest(1e20f) << '\n'; // Want to keep as is
std::cout << to_string_shortest(1e-20f) << '\n'; // Want to keep as is
}