s = "pi=%7.1e" % acos(-1)julia> s = @printf("%7.1e", 3.14)
3.1e+00
julia> s
julia> fmt = "%8.1e";
julia> @sprintf(fmt, 3.1415)
ERROR: first or second argument must be a format string
julia> @sprintf("%8.1e", 3.1415)That @sprintf is a macro sort of explains why using a run-time value doesn't work in the same way, but it isn't really the reason since @sprintf(fmt, val) could work in principle – it would just have to delegate to a function if its argument isn't a compile-time string.
If using a run-time string is particularly useful to you, I'd suggest opening an issue about this, since it appears to be missing functionality.
julia> X=[1 2]
1x2 Array{Int64,2}:
1 2
julia> @sprintf("%d%d",1,2)
"12"
julia> @sprintf("%d%d",X...)
ERROR: @sprintf: wrong number of arguments
julia> @sprintf("%d%d",(1,2)...)
ERROR: @sprintf: wrong number of arguments
julia> @sprintf("%d",X...)
ERROR: error compiling anonymous: unsupported or misplaced expression ... in function anonymous
in sprint at io.jl:460
in sprint at io.jl:464
julia> macroexpand(quote @sprintf("%d%d",X...) end)
:($(Expr(:error, ErrorException("@sprintf: wrong number of arguments"))))
julia> fmt = "%8.1e""%8.1e"julia> @eval dofmt(x) = @sprintf($fmt, x)dofmt (generic function with 1 method)julia> dofmt(1)" 1.0e+00"julia> dofmt(123.456)" 1.2e+02"
Up for grabs issue: https://github.com/JuliaLang/julia/issues/6520. If anyone is interested in doing a bit of metaprogramming, this is a good opportunity.
julia> argsʹ = tuple(args...)(1,2,3,4,5)
macro eval libc
8.06e-05 5.91e-02 6.63e-03