Thanks, in my intended application I will happily pay the performance penalty as long as it’s less than about 0.1s. Your solution seems to solve the problem just fine without the need for an additional package, thanks!
I am personally not very fond of Formatting.jl - not enough lean
A short (and clean i hope) solution can be to gen closure with a macro according to the classical show signature
:((io::IO, arg) -> Printf.@printf(io, $fmt, arg))
# can be used like this
const fi = @gprintf "%d"
const f3p = @gprintf "%.3f"
const f6p = @gprintf "%.6f"
jit_printf(fmt) = @eval @gprintf($fmt)
@testset "printf_functionalization" begin
@test sprint(f3p, 3.14) == "3.140"
@test sprint(f6p, 3.14) == "3.140000"
@testset for i in 3:8
fmt = string("%.", i, "f")
fjp = @eval @gprintf($fmt) # <=> fjp = jit_printf(fmt)
@test sprint(fjp, 3.14) == "3.14" * repeat('0', i-2)
PS: BTW @RaulDurand that fix your point, no external package, no eval