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", √π)
6 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?
4 Likes