4

Is it possible to format number with thousands separator using fmt?

e.g. something like this:

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

update

I am researching fmt, so I am looking for solution that works with fmt library only, without modify locale in any way.

4

2 回答 2

3

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

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

这打印:

10,000

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

于 2019-11-19T17:10:55.887 回答
1

根据fmt API 参考

使用“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;
}
于 2021-07-02T14:17:02.640 回答