User Tools

Site Tools


cs326:formatted_output

This is an old revision of the document!


Formatted Output using :io.format

The Erlang library :io includes a format function that works in a way analogous to System.out.printf, but of course the details are different…very different.

We'll explore it in a series of examples:

iex> :io.format("hello world~n", [])
hello world
:ok

iex> :io.format("this outputs one Elixir number: ~b~n", [-999])
this outputs one Elixir term: -999
:ok

iex> :io.format("this outputs one Elixir term: ~w~n", [:hello])
this outputs one Elixir term: hello
:ok

iex> :io.format("this outputs one Elixir term: ~w~n", ["hello"])
this outputs one Elixir term: <<104,101,108,108,111>>
:ok

iex> :io.format("this outputs two Elixir terms: ~w ~w~n", [:hello, :world])
this outputs two Elixir terms: hello world
:ok

iex> :io.format("~-15w: ~b degrees C~n", [:moscow, -10]) # ~-15w sets a 15-character field, left justified
moscow         : -10 degrees C
:ok

iex> :io.format("list: ~p, tuple: ~p~n", [[1,2,3], {1,2}])
list: [1,2,3], tuple: {1,2}
:ok

cs326/formatted_output.1518731343.txt.gz · Last modified: 2018/02/15 15:49 by scarl