Nicely formatting numbers in C++

c++

Solution

As of C++14, you can use `'` as a digit group separator:

auto one_m = 1'000'000;

Previous versions of C++ did not support this natively. There were two major workarounds:

Using user-defined literals in C++11; this would allow you to write code as follows:

auto x = "1_000_000"_i;

(Writing this as a `constexpr` would be trickier – but is definitely possible.)

Using a straightforward macro, which would allow the following code:

  auto x = NUM(1,000,000);

Problem

In Ada it is possible to write numbers with underscores for separating digits, which greatly improves readability. For example: 1_000_000 (which is equivalent to 1000000) Is there some similar way for C++? EDIT: This is question about source code, not I/O.

Original source

Related problems