User Tools

Site Tools


cs326:formatted_output

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Last revision Both sides next revision
cs326:formatted_output [2018/02/16 14:46]
scarl
cs326:formatted_output [2018/02/16 14:56]
scarl
Line 1: Line 1:
 ==== Formatted Output using :io.format ==== ==== 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. One thing in particular, that I tend to forget: the ''​format''​ function takes two arguments: the format string and a list of zero or more variables and literals to be output.+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. One thing in particular, that I tend to forget: the ''​format''​ function takes two arguments: the format string and a //list// of zero or more variables and literals to be output.
  
 We'll explore it in a series of examples: We'll explore it in a series of examples:
Line 10: Line 10:
 :ok :ok
  
-iex> :​io.format("​this outputs ​one Elixir number: ~b~n", [-999]) +iex> :​io.format("​Outputs ​one Elixir number: ~b~n", [-999]) 
-this outputs ​one Elixir ​term: -999+Outputs ​one Elixir ​number: -999
 :ok :ok
  
-iex> :​io.format("​this outputs ​a floating-point number: ~f~n", [5.678]) +iex> :​io.format("​Outputs ​a floating-point number: ~f~n", [5.678]) 
-The number ​is 5.678+Outputs a floating-point ​number5.678000
 :ok :ok
  
-iex> :​io.format("​this outputs ​the same but with just one digit to the right of the decimal: ~3.1f~n", [5.678]) +iex> :​io.format("​Outputs ​the same but with just 3 digits ​to the right of the decimal: ~.3f~n", [5.678]) 
-The number is 5.7+Outputs the same but with just 3 digits to the right of the decimal: ​5.678
 :ok :ok
  
-iex> :​io.format("​this outputs ​a floating-point number on a line by itself:~n ~3.1f~n",​ [5.678]) +iex> :​io.format("​Outputs the same but with field width 3 and just 1 digit right of the decimal: ~3.1f~n",​ [5.678]) 
-The number ​is  +Outputs the same but with field width 3 and just 1 digit right of the decimal: 5.7 
-5.7+:ok 
 + 
 +iex> :​io.format("​Outputs ​a floating-point number on a line by itself:~n ~3.1f~n",​ [5.678]) 
 +Outputs a floating-point ​number ​on a line by itself: 
 + 5.7
 :ok :ok
  
 iex> :​io.format("​this outputs one Elixir term: ~w~n", [:hello]) iex> :​io.format("​this outputs one Elixir term: ~w~n", [:hello])
 this outputs one Elixir term: 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 :ok
  
Line 38: Line 38:
 this outputs two Elixir terms: hello world this outputs two Elixir terms: hello world
 :ok :ok
 +
 +iex> :​io.format("​~~w outputs any Elixir term: ~w~n", [:​hello]) ​    
 +~w outputs any Elixir term: hello
 +:ok
 +
 +iex> :​io.format("​~~w outputs any Elixir term: ~w~n", ["​hello"​])
 +~w outputs any Elixir term: <<​104,​101,​108,​108,​111>>​
 +:ok
 +
 +iex> :​io.format("​~~w outputs any Elixir term: ~w~n", [-999]) ​  
 +~w outputs any Elixir term: -999
 +:ok
 +
 +iex> :​io.format("​~~w outputs any Elixir term: ~w~n", [[1, 2, 3]])
 +~w outputs any Elixir term: [1,2,3]
 +:ok
 +iex> ​
  
 iex> :​io.format("​~-15w:​ ~b degrees C~n", [:moscow, -10]) # ~-15w sets a 15-character field, left justified iex> :​io.format("​~-15w:​ ~b degrees C~n", [:moscow, -10]) # ~-15w sets a 15-character field, left justified
Line 48: Line 65:
 </​code>​ </​code>​
  
-Complete docs on the :io.format function are on the Erlang ​library ​website, but be warned, it is extensive. Note that in Erlang the function is invoked as io::​format(...)  ​+Complete docs on the :io.format function are on the [[http://​erlang.org/​doc/​man/​io.html#​format-2|Erlang ​STDLIB ​website]], but be warned, it is extensive. Note that in Erlang the function is invoked as io::​format(...)  ​
cs326/formatted_output.txt · Last modified: 2018/02/16 15:00 by scarl