String interpolation formatting

Consider

println("The square root of pi is $(sqrt(pi))")

which displays

The square root of pi is 1.7724538509055159

How can I change this code if I only want to print the answer to (say) two decimal places?

I know that there exist packages that provide macros like @sprintf, but that seems complicated and inconvenient for such a simple task.

I would expect this sort of thing to be built into the string interpolation syntax like it is in Python, so mostly I want to check that it isn’t.

1 Like
println("The square root of pi is $(round(sqrt(pi),digits=2))")
1 Like

That works as a hack, but how would you do zero padding, for example?

Agree to disagree:

using Printf
println("The square root of pi is $(@sprintf("%07.2f", √π))")

Actually better written as:

@printf("The square root of pi is %07.2f\n", √π)
7 Likes

I know that there exist packages that provide macros like @sprintf, but that seems complicated and inconvenient for such a simple task.

Well, that package is in the stdlib.

https://docs.julialang.org/en/v1/stdlib/Printf/#man-printf

2 Likes

but … the whole point of string interpolation is not to have to use the inconvenient printf syntax where the variables appear at the end instead of at the place where they’re used. So for me the @sprintf version is much better than the @printf one, but it absolutely isn’t readable or convenient to type.

Is PyFormattedStrings closer to what you’re looking for?

5 Likes

Another solution would be to use StrFormat, and you can write it as:
println(f"The square root of pi is \%07.2f(√π)")
or (a short form, useful for when you are inserting debugging print statements)
pr"The square root of pi is \%07.2f(√π)"

1 Like

I’d probably avoid the JuliaString packages for now until they’ve undergone more rigorous testing.

1 Like

That bug you are referring to is one I inherited from the Formatting.jl package - and it only affects the use of the Python style printfmt and FormatSpec interface - not the code used by the C printf style formatting used by StrFormat (which is pretty much identical to the formatting code in stdlib/Printf.jl)
The JuliaString packages have been fairly rigorously tested (in many cases there’s a lot more testing of the string functions than in base for String), although it’s true I hadn’t done much to write additional tests for the Python style formatting from the JuliaIO/Formatting.jl package (not being a Python programmer myself)

1 Like

There’s also GitHub - magonser/FStrings.jl to do the same. It has a nicer name but I don’t know how it compares in features.

I’m not sure if that’s being maintained - the last change was from 2 years ago

That seems like a reason to also not use JuliaIO/Formatting.jl, rather than a reason to use Format.jl. It seems the Printf stdlib does not have such egregious bugs (/ when it does, they are fixed) and so it should be preferred over both.

All of the bugs that had been found in JuliaIO/Formatting.jl have now been fixed in JuliaString/Format.jl, as well as a number features in Python formatted strings have now been implemented as well (such as center justification, with ^, using , or _ to group digits, and precision to truncate strings).

Note that Printf does have some bugs handling precision to truncate strings (in particular, when there are wide characters in the string)

3 Likes