Nick
Nick

Reputation: 10539

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

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.

Upvotes: 7

Views: 3310

Answers (2)

Nick
Nick

Reputation: 10539

I found the answer online in Russian forum:

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

This prints:

10,000

The thousands separator is locale depended and if you want to change it to something else, only then you need to "tinker" with locale classes.

Upvotes: 6

Lejuanjowski
Lejuanjowski

Reputation: 169

According to the fmt API reference:

Use the 'L' format specifier to insert the appropriate number separator characters from the locale. Note that all formatting is locale-independent by default.

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

Upvotes: 5

Related Questions