typestar

Directivas de formato en Erlang

~s, ~p, ~B, ~.2f: el vocabulario de io:format.

%% 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]).

Cómo funciona

  1. ~B imprime enteros; ~.2f fija decimales de flotantes.
  2. ~p imprime bonito cualquier término, con comillas donde toca.
  3. io_lib:format devuelve el texto en vez de imprimirlo.

El intento, en números

Líneas
15
Caracteres a escribir
481
Tokens
119
Ritmo de tres estrellas
60 tpm

Al ritmo de tres estrellas de 60 tokens por minuto, este intento toma unos 119 segundos.

Escribe este fragmento

Paso 2 de 4 en Texto y números; paso 20 de 31 en Fundamentos del lenguaje.

← Anterior Siguiente →