typestar

Printf in Julia

C-style formatting from the standard library.

using Printf

targets = [("Vega", 0.03), ("Polaris", 1.98), ("Deneb", 1.25)]

# @printf formats in place; %-8s pads, %5.2f fixes decimals.
println("target   mag")
for (name, mag) in targets
    @printf("%-8s %5.2f\n", name, mag)
end

# @sprintf returns the string instead of printing it.
line = @sprintf("Brightest: %s (%.2f)", targets[1][1], targets[1][2])
println(line)

How it works

  1. using Printf brings the macros in.
  2. @printf("%-8s %5.2f\n", ...) pads and fixes decimals.
  3. @sprintf returns the formatted string instead.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
367
Tokens
90
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 83 seconds.

Type this snippet

Step 2 of 3 in Macros & formatting, step 23 of 27 in Language basics.

← Previous Next →