0

For percentage in form "x.%x", the first x <= 3 bits, the second x == 2 bits

a1 = 93.11;
a2 = 33.72;
a3 = 30.69;
b1 = 0.00;
b2 = 0.00;
b3 = 0.00;
printf("%3.2f%% %3.2f%% %3.2f%%\n", a1, a2, a3); // wrong, how to modify?
printf("%3.2f%% %3.2f%% %3.2f%%\n", b1, b2, b3);

Output:
93.11% 33.72% 30.69%
0.00% 0.00% 0.00%
But the output is not right. How to align to make output below:

enter image description here

abelenky
  • 63,815
  • 23
  • 109
  • 159
Hel
  • 305
  • 5
  • 14
  • you just want an extra leading on trailing zero no? I mean the issue is there is just one char worth of whitespace in there. – Ashwin Gupta Dec 01 '16 at 01:57
  • @Ashwin Gupta I've added an image. – Hel Dec 01 '16 at 01:59
  • I got you, but how do you intend to fill it? 0.00% has one less char then 93.11%, you need to fill it with something. Either whitespace, or a leading/trailing zero? – Ashwin Gupta Dec 01 '16 at 02:00
  • 3
    You did not read the documentation of the functions you use, did you? – too honest for this site Dec 01 '16 at 02:00
  • Or look very hard to see if there was an existing question that provided the information you need. Possible duplicate of [Aligning printf() variables and decimals in C](http://stackoverflow.com/questions/19202242/aligning-printf-variables-and-decimals-in-c) – Ken White Dec 01 '16 at 02:08
  • @Olaf I've read it before, but not very carefully. I'll read it again. – Hel Dec 01 '16 at 02:17

1 Answers1

3

I believe you want:

printf("%5.2f%% %5.2f%% %5.2f%%\n", a1, a2, a3);
printf("%5.2f%% %5.2f%% %5.2f%%\n", b1, b2, b3);

The 5 means, print at least 5 characters.

12345
93.11  <== 5 total characters, counting the decimal point.
 0.00  <== 5 total characters, first char is a [space]

Link to IDEOne Code

The output is:

93.11% 33.72% 30.69%
 0.00%  0.00%  0.00%
abelenky
  • 63,815
  • 23
  • 109
  • 159