Formatting.jl icon indicating copy to clipboard operation
Formatting.jl copied to clipboard

`sprintf1` can format C-style format specification string like `"p=%.1f"` in Julia 1.6+ but cannot do it on Julia 1.4

Open singularitti opened this issue 4 years ago • 0 comments

As the title suggests, I want to print a string like "p=20.0" using sprintf1 with a C-style format specification string. For example,

julia> using Formatting, Printf

julia> sprintf1("p=%.1f", 20)
ERROR: Only one AND undecorated format string is allowed
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] checkfmt at /Users/qz/.julia/packages/Formatting/BwWBf/src/cformat.jl:12 [inlined]
 [3] generate_formatter(::String) at /Users/qz/.julia/packages/Formatting/BwWBf/src/cformat.jl:23
 [4] sprintf1(::String, ::Int64) at /Users/qz/.julia/packages/Formatting/BwWBf/src/cformat.jl:3
 [5] top-level scope at REPL[12]:1

julia> @sprintf("p=%.1f", 20)
"p=20.0"

julia> versioninfo()
Julia Version 1.4.2
Commit 44fa15b150* (2020-05-23 18:35 UTC)

But it does work on Julia 1.6+:

julia> sprintf1("p=%.1f", 20)
"p=20.0"

julia> versioninfo()
Julia Version 1.7.2
Commit bf53498635 (2022-02-06 15:21 UTC)

As you can see, @sprintf actually has a wider range of "%Fmt" that it can take. Can I still do that with a C-style format specification string in Formatting.jl?

singularitti avatar Apr 18 '22 07:04 singularitti