How to repeat the format in @printf?

For the record, probably the best solution here is just write a double loop: Writing array to file with format - #2 by stevengj

julia> using Printf

julia> A = rand(3,3);

julia> # with io == stdout
       for i in axes(A,1)
           for j in axes(A,2)
               @printf(" %5.2f", A[i,j])
           end
           println() # new line
       end
  0.21  0.48  0.26
  0.12  0.92  0.17
  0.03  0.91  0.41

or to a file:

julia> open("test.txt","w") do io
           for i in axes(A,1)
               for j in axes(A,2)
                   @printf(io, " %5.2f", A[i,j])
               end
               println(io) # new line
           end
       end

shell> more test.txt
  0.21  0.48  0.26
  0.12  0.92  0.17
  0.03  0.91  0.41
2 Likes