Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom format to String #86

Open
junder873 opened this issue Jun 4, 2024 · 1 comment
Open

Custom format to String #86

junder873 opened this issue Jun 4, 2024 · 1 comment

Comments

@junder873
Copy link

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
Copy link
Member

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)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants