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)
Jules G.M.
  • 3,624
  • 1
  • 21
  • 35
Joshua Cook
  • 12,495
  • 2
  • 35
  • 31

4 Answers4

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
niczky12
  • 4,953
  • 1
  • 24
  • 34
  • From my experience, in the Julia 1.0.0 REPL you have to add: using Printf, before employing the @sprintf macro. – Julia Learner Sep 09 '18 at 20:33
  • Thanks! I change the answer to reflect the 1.0 changes. – niczky12 Sep 10 '18 at 08:08
  • 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. – Julia Learner Sep 10 '18 at 11:58
22

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 
Daniel Arndt
  • 2,268
  • 2
  • 17
  • 22
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
Scott Jones
  • 1,750
  • 14
  • 14
0

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
  • 2
    Didn't the OP state that this works? OP added though that this doesn't seem stylistically correct. – AKdemy Jul 04 '23 at 21:00