I am writing a short script in Julia, and I need to print a bunch of floating point numbers for the user. If I just print("my number: $f"), I get output like "my number: 2.99999999999" which is too noisy. I want a human-friendly format. A good approximation would be just using two digits after the point. Is there convenient way to do this? I can use round(f; digits = 2), but that’s a lot of typing. I am looking at something a-la {f:.2}.
There are ways to combine values and string pieces:
Using string interpolation: "elapsed: $(f)s"
Using sprintf: @sprintf "number: %.2fs" f
The first is more convenient, as you don’t have to match format string with n arguments. The second one allows formatting. Ideally, it should be possible to have both. In Python, for example, it is possible to write f"elapsed: {f:.2}s".
Oh, I get it. Nope, it seems there is no alternative for that (that I know of), but I guess it should be relatively easy to make such thing.
If want to retain interpolation but you don’t need as much control over how it is printed, you can always use IOContext property :compact = true (see examples in I/O and Network · The Julia Language)