JuliaString / Format.jl

A Julia package to provide C and Python-like formatting support
Other
40 stars 6 forks source link

Custom format to String #86

Open junder873 opened 4 months ago

junder873 commented 4 months ago

If I want to create a string from a custom format, it seems like I am supposed to use cfmt or format, but these do not have complete flexibility.

For example, if I want to create a string for $\pi$ surrounded by parentheses ((3.14)), how would I do that? printfmtln will display it: printfmtln("({:0.2f})", π), but this returns nothing (so it cannot be used later in the program). I tried cfmt but cfmt("(%0.2f)", π) causes an error. It is possible through format by running format(-π; parens=true) but this is not always work if I want something else besides parentheses.

With Printf, this is possible: Printf.format(Printf.Format("(%0.2f)"), π) will return a string. So can cfmt be extended to accept similar options?

ScottPJones commented 3 months ago

It would probably be better to use StrFormat.jl, which adds formatting (as well as other functionality, both with C style and Python style, provided by Format.jl) to interpolated strings.

julia> using StrFormat
Precompiling StrFormat...
  3 dependencies successfully precompiled in 4 seconds. 1 already precompiled.

julia> f"(\{0.2f}(pi))"
"(3.14)"