无法使用 {fmt} 库在一个字符串中格式化两个浮点数

Cannot format two floating point numbers in one string using the {fmt} library

我刚开始在我的应用程序中使用 {fmt} 库,发现我无法使用该库来格式化两个位数不同的浮点数,因为程序崩溃了。

经过一些实验,我意识到它实际上有点糟糕,因为在用 {0:.0f}(或 0:.2f,就此而言)。

对我来说违反直觉的代码示例:

#include <fmt\core.h>
#include <iostream>

int main()
{
    std::cout << fmt::format("{} , {}\n", 3.14, 10.0); // Prints out '3.14, 10.0'
    //std::cout << fmt::format("{0:.0f} , {}\n", 3.14, 10.0); // - ERROR: fmt::v6::format_error at memory location 
    std::cout << fmt::format("{0:.0f} , {0:.0f}\n", 3.14, 10.0); // - WRONG RESULT: Prints out '3, 3'
    std::cout << fmt::format("{0:.0f} , {:d}\n", 3.14, 10); // ERROR: fmt::v6::format_error at memory location

    //std::cout << fmt::format("{:s}, {:s}", fmt::format("{0:.2f}", 3.14), fmt::format("{:0:.1f}", 10.0)); // EVEN THIS DOESN'T WORK

    // This is the only way I found of getting the output I want:
    std::string a = fmt::format("{0:.2f}", 3.14);
    std::string b = fmt::format("{0:.1f}", 10.0);

    std::cout << fmt::format("{:s}, {:s}", a, b);

    return 0;
}

:之前的数字用于计算参数。
0:是第一个,1:第二个...
如果您不在 : 之前放置任何内容,那么将按顺序考虑参数。
您不能在相同的格式字符串中混用一些 {} 带有参数计数器而另一些没有这样的参数计数器。