How to use @sprintf with "parametric" format string?

Is there a way to use @sprintf with “parametric” format string? or it has to be hardcoded?
This is an example:

julia> @sprintf(“test: %s”, “test”)
“test: test”
julia> fmt = “test: %s”; @sprintf(fmt, “test”)
ERROR: LoadError: ArgumentError: @sprintf: first argument must be a format string

In Julia 1.6 you can do this:

julia> import Printf: Format, format

julia> function test(n,x)
           f = Format("%10.$(n)f")
test (generic function with 1 method)

julia> test(3,1.0)
"     1.000"

julia> test(5,1.0)
"   1.00000"

There is also the Formatting.jl package: GitHub - JuliaIO/Formatting.jl: A Julia package to provide Python-like formatting support

1 Like