【问题标题】:Is it possible to format number with thousands separator using fmt?是否可以使用 fmt 格式化带有千位分隔符的数字?
【发布时间】:2020-03-15 05:28:56
【问题描述】:

是否可以使用 fmt 格式化带有千位分隔符的数字?

例如像这样:

int count = 10000;
fmt::print("{:10}\n", count);

更新

我正在研究 fmt,因此我正在寻找仅适用于 fmt 库的解决方案,无需以任何方式修改语言环境。

【问题讨论】:

标签: c++ fmt


【解决方案1】:

我在俄罗斯论坛网上找到了答案:

int count = 10000;
fmt::print("{:10L}\n", count);

打印出来:

10,000

千位分隔符取决于语言环境,如果您想将其更改为其他内容,那么您需要“修改”语言环境类。

【讨论】:

    【解决方案2】:

    根据fmt API reference

    使用“L”格式说明符从区域设置中插入适当的数字分隔符。 请注意,默认情况下,所有格式都与语言环境无关。

    #include <fmt/core.h>
    #include <locale>
    
    int main() {
      std::locale::global(std::locale("es_CO.UTF-8"));
      auto s = fmt::format("{:L}", 1'000'000);  // s == "1.000.000"
      fmt::print("{}\n", s);                    // "1.000.000"
      return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 2012-03-11
      • 2015-02-28
      • 2012-02-28
      • 2018-05-11
      • 1970-01-01
      相关资源
      最近更新 更多