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
julia> import Printf: Format, format
julia> function test(n,x)
f = Format("%10.$(n)f")
format(f,x)
end
test (generic function with 1 method)
julia> test(3,1.0)
" 1.000"
julia> test(5,1.0)
" 1.00000"
(However, it’s faster if you use a literal format string ala @sprintf, since that allows the format to be parsed at compile time. What are you trying to accomplish?)
Probably because you should really be using @printf if you can (or the Printf.format"foo" string macro), due to the aforementioned compile-time format parsing. A runtime-constructed format string should be a rarity in practice.