typestar

Format directives in Erlang

~s, ~p, ~B, ~.2f: the io:format vocabulary.

%% io:format directives: ~s strings, ~p terms, ~B integers, ~.2f.
main(_) ->
    Operator = "Maud",
    Calls = 47,
    Uptime = 99.9625,

    io:format("~s connected ~B calls~n", [Operator, Calls]),
    io:format("Uptime: ~.2f percent~n", [Uptime]),

    %% ~p pretty-prints any term, quoting where needed.
    io:format("Raw term: ~p~n", [{shift, night, [3, 7]}]),

    %% io_lib:format builds the string instead of printing.
    Label = io_lib:format("~s-~B", [Operator, Calls]),
    io:format("Label: ~s~n", [Label]).

How it works

  1. ~B prints integers; ~.2f fixes float decimals.
  2. ~p pretty-prints any term with quoting.
  3. io_lib:format returns the text instead of printing it.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
481
Tokens
119
Three-star pace
60 tpm

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

Type this snippet

Step 2 of 4 in Text & numbers, step 20 of 31 in Language basics.

← Previous Next →