在可能的情况下,如何缩小浮动的字符串表示?

问题描述 投票:0回答:1
我研究

std::format

试图使浮点变量的简短表示。
我的问题是我无法设置宽度,因为我不知道小数点前有多少位数字。如果我设置了精度,即使它们是唯一的尾巴数字,它也会打印零。
我猜想这可以通过宽度/精度/填充等以某种方式实现,但看不到道路。
llet说,我希望字符串适合指定的
std::string
如果可能的话;如果不是,字符串应采用最小的数字,则优先使用“ 100”而不是“ 1E2”或“ 1000”或1E03“或“ 0.01”至“ 1E-2”,等等。

可以通过std ::格式(或任何其他类型的std格式)来实现这一点,而无需手动转换float的每个数字?

fit_to

demo

.

    

以下版本的

#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.

c++ formatting printf std
1个回答
1
投票
[]

-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 }

	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.