How do you format a string when interpolated in Julia?
Asked Answered
H

4

35

In Python 3, I would

print_me = "Look at this significant figure formatted number: {:.2f}!".format(floating_point_number)
print(print_me)

or

print_me = f"Look at this significant figure formatted number: {floating_point_number:.2f}!"
print(print_me)

In Julia

print_me = "Look at this significant figure formatted number: $floating_point_number"
print(print_me)

but this would yield say

Look at this significant figure formatted number: 61.61616161616161

How do I get Julia to restrict the number of decimal places it displays? Note that the necessary storage of the string to be printed, to my knowledge, rules out using the @printf macro.

This works, but does not seem stylistically correct.

floating_point_number = round(floating_point_number,2)
print_me = "Look at this significant figure formatted number: $floating_point_number"
print(print_me)
Hoffarth answered 4/5, 2016 at 14:46 Comment(0)
A
36

You can use @sprintf macro from the standard library package Printf. This returns a string rather than just printing it as @printf.

using Printf
x = 1.77715
print("I'm long: $x, but I'm alright: $(@sprintf("%.2f", x))")

Output:

I'm long: 1.77715, but I'm alright: 1.78
Accredit answered 4/5, 2016 at 15:3 Comment(3)
From my experience, in the Julia 1.0.0 REPL you have to add: using Printf, before employing the @sprintf macro.Porphyrin
Thanks! I change the answer to reflect the 1.0 changes.Accredit
Thanks for updating your accepted answer. It will help newbies to Julia who might not know that the @sprintf macro is not built into the core Julia language. I just came to Julia myself, at 1.0.0, and assume the Julia code you previously showed worked for earlier versions of Julia.Porphyrin
A
23

In addition to @niczky12's answer you can also use the Formatting package designed precisely for this type of thing!

Pkg.add("Formatting")
using Formatting: printfmt
x = 1.77715
printfmt("I'm long: $x, but I'm alright: {:.2f}", x)

Output:

I'm long: 1.77715, but I'm alright: 1.78 
Autography answered 4/5, 2016 at 15:35 Comment(0)
B
6

While it is still a work in progress (I need to add a bunch of unit tests, and I want to add a Python 3.6 style mode), you could also use my StringUtils.jl package, which adds C and Python like formatting, Swift style interpolation, Emoji, LaTex, and Html and Unicode named characters to string literals.

Pkg.clone("https://github.com/ScottPJones/StringUtils.jl")
Pkg.checkout("StringUtils")

using StringUtils

x = 1.77715
print(u"I'm long: \(x), but I'm alright: \%.2f(x)")

Output:

I'm long: 1.77715, but I'm alright: 1.78
Binford answered 4/5, 2016 at 21:56 Comment(0)
S
1

Your original solution works in Julia 1.8 without any extra imports. It can round to specified significant figures as follows.

floating_point_number = 100.457894
rounded_floating_point_number = round(floating_point_number,digits=2)
print_me = "Look at this significant figure formatted number: $rounded_floating_point_number"
println(print_me)

Output

Look at this significant figure formatted number: 100.46
Silicosis answered 4/7, 2023 at 17:5 Comment(1)
Didn't the OP state that this works? OP added though that this doesn't seem stylistically correct.Languet

© 2022 - 2024 — McMap. All rights reserved.