printf format float with padding

The following should line everything up correctly:

printf("ABC %6.1f DEF\n", test);
printf("ABC %6.1f DEF\n", test2);

When I run this, I get:

ABC 1234.5 DEF
ABC   14.5 DEF

The issue is that, in %5.1f, the 5 is the number of characters allocated for the entire number, and 1234.5 takes more than five characters. This results in misalignment with 14.5, which does fit in five characters.

Leave a Comment