4

Wondering if I did something like

printf("%04.2f", float_variable);

Could I have it print

" 1.15"

Instead of

"01.15"

Weird request I know, but it is what I need. No idea how to start.

Zack
  • 13,454
  • 24
  • 75
  • 113

1 Answers1

8

Just drop the 0, which, by definition, means the number will be zero padded:

printf("%5.2f", float_variable);

Notice that the width of the field is five instead of four, since the value is equal to the minimum width of the entire output string, not just the number of digits.

  • Yes. To elaborate, space padding is the default. `0` is a flag character that means to pad on the left with zeroes instead of spaces. – hobbs Dec 12 '13 at 01:19