@printf not consistent with C printf?

quick question about %02.3f format specifier - that should left pad zeros to the left of the comma sign, and have 3 digits afterwards, like here formatting - 'printf' with leading zeros in C - Stack Overflow

julia> @printf("%02.3f" ,π)
3.142  # I wanted 03.142

Try @printf "%06.3f" π (the field size specifiers seems to indicate the min length of the entire stringified float, not the integer part…), which I find unexpected too.

2 Likes

That’s the correct behavior. As @ess3sq says the field width (the number you give before the .) specifies the width of the entire field. This is generally what you want for creating nicely aligned ASCII tables:

julia> v = rand(5);

julia> for x in v
         @printf "%08.3f\n" 10^3x
       end
0019.654
0009.578
0007.408
0026.634
0507.169

or with spaces instead of 0:

julia> for x in v
         @printf "%8.3f\n" 10^3x
       end
  19.654
   9.578
   7.408
  26.634
 507.169

a yes i see - my bad! thanks