4

It has been a while since I posted any questions on StackOverflow; I hope I remember more or less the appropriate way to address a question (sorry in advance).

I was playing with C++ streams and the FMT library, which provides a preview to C++23 print. Streams are chill when setting fillers to complete the display width of an integer. Example:

const int y = 730;
cout << "y = " << setw(5) << y << endl;
cout << "y = " << setfill('0') << setw(5) << y << endl;
cout << "y = " << setfill('X') << setw(5) << y << endl;
cout << "y = " << setfill('*') << setw(5) << y << endl;

The output is

y =   730
y = 00730
y = XX730
y = **730

I was trying to set the same fillers (including 'X' and '*' chars) using fmt::print—something like:

print("y = {:5d}\n", y);
print("y = {:05d}\n", y);

Well, I failed to include 'X' and '*'. I am not claiming it is a good idea to have random chars as fillers; I am just curious as I need to explain the difference between these commands to students.

Thanks in advance. Vivian

Vivian Miranda
  • 2,467
  • 1
  • 17
  • 27
  • 2
    Try adding the alignment specifier: https://godbolt.org/z/oaadTx5nY – Bob__ Mar 29 '23 at 18:31
  • 2
    The best thing you can do for such questions is to provide a piece of code we can run as is (with the right headers, with `main()`, etc). If there's also a godbolt link to quickly run it, that's a huge plus. – HolyBlackCat Mar 29 '23 at 18:38

2 Answers2

6

The manual shows following grammar for integer format specifiers:

format_spec ::=  [[fill]align][sign]["#"]["0"][width]["." precision]["L"][type]
fill        ::=  <a character other than '{' or '}'>
align       ::=  "<" | ">" | "^"

Here format_spec is the part inside of {: }.

So specifying a custom filler (fill) also requires specifying alignment (you want >, i.e. align to the right):

fmt::print("y = {:X>5d}\n", y); // XX730
fmt::print("y = {:*>5d}\n", y); // **730
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
5

The alignment is mandatory when a filling character is given.

print("y = {:*>5}\n", y); 
dalfaB
  • 436
  • 1
  • 7