format/[2,3]
format(
+Control, +Arguments)
format(
+Stream, +Control, +Arguments)
Interprets the Arguments according to the Control string and prints the result on the current or specified output stream.
~
<n>
<c>
Please note: In the case where there is only one argument and that argument is a list, then that argument must be enclosed in a list.
If <n> can be specified, then it can be the character *
.
In this case <n> will be taken as the next argument
from Arguments.
The following control options cause formatted printing of the next
element from Arguments to the current output stream. The
argument must be of the type specified, or format/1
will raise a
consistency error.
~
<n>a
| ?- format('~a', foo). foo
~
<n>c
| ?- format('~2c', 97). aa
~
<n>e
-
,
.
if <n> is greater than 0,
e
,
+
or a -
, and
If <n> is omitted, it defaults to 6.
| ?- format('~3e', 1.33333). 1.333e+00
See ref-ari-ove for detailed information on precision.
Notes:
~
<n>e
coerces integers to floats
~
<n>E
E
is used for exponentiation instead of e
.
| ?- format('~3E', 1.33333). 1.333E+00
~
<n>f
| ?- format('~3f', 1.33333). 1.333
Notes:
~
<n>f
coerces integers to floats
See the section on floating-point arithmetic for detailed information on
precision.
~
<n>g
| ?- format('~g', 1000000000.0). 1e+09 | ?- format('~20g', 1000000000.0). 1000000000
See the section on floating-point arithmetic for detailed information on
precision.
~
<n>G
E
is used for exponentiation instead of e
.
| ?- format('~G', 1000000.0). 1E+06
~
<n>d
| ?- format('~d', 29). 29 | ?- format('~1d', 29). 2.9
~
<n>D
| ?- format('~1D', 29876). 2,987.6
~
<n>r
a-z
.
If <n> is omitted, it defaults to 8.
| ?- format('~2r', 13). 1101 | ?- format('~r', 13). 15 | ?- format('~16r', 13). d
~
<n>R
A-Z
instead of a-z
.
| ?- format('~16R', 13). D
~
<n>s
| ?- format('~s', ["string"]). string | ?- format('~3s', ["string"]). str | ?- format('~a', "string"). ! Consistency error: a and [115,116,114,105,110,103] are inconsistent ! the argument for the format control option "a" must be of type "atom". ! goal: format('~a', [115,116,114,105,110,103])
The following control options can take an argument of any type:
~i
| ?- format('~i', 10).
~k
write_canonical/[1,2]
.
| ?- format('~k', 'A'+'B'). +('A','B')
~p
print/[1,2]
.
| ?- asserta((portray(X+Y) :- write(X), write(' plus '), write(Y))). | ?- format('~p', 'A'+'B'). A plus B
~q
writeq/[1,2]
.
| ?- format('~q', 'A'+'B'). 'A'+'B'
~w
write/[1,2]
.
| ?- format('~w', 'A'+'B'). A+B
The following control options do not have a corresponding argument:
~~
~
.
| ?- format('~~', []). ~
~
<n>n
| ?- format('begin~nend', []). begin end
~N
| ?- format('~Nbegin~N~Nend', []). begin end
The following control options manipulate column boundaries (tab positions). These column boundaries only apply to the line currently being written. A column boundary is initially assumed to be in line position 0.
~
<n>|
~
<n>+
~
<n>t
~t
's, if any, in the column, and each ~t
fills its allotted space with characters of ASCII code <n>.
If <n> is omitted, it defaults to ASCII 32 (space).
<n> can also be of the form `<c>, where <c> is the fill character.
See extended example below.
Stream errors (see ref-iou-sfh-est), plus:
consistency_error
domain_error
1. The following is an extended
example of the use of format/[2,3]
and the character escaping
facility.
| ?- prolog_flag(character_escapes, _, on). yes | ?- compile(user). | toc(Rel) :- format('Table of Contents ~t ~a~72|~*n', [i,3]), format('~tTable of Contents~t~72|~*n', 2), format("1. Documentation supplement for ~s~1f \c ~`.t ~d~72|~*n", ["Quintus Prolog Release ",Rel,2,2]), format("~t~*+~w Definition of the term \"loaded\" \c ~`.t ~d~72|~n", [3,1-1,2]), format("~t~*+~w Finding all solutions ~`.t ~d~72|~n", [3,1-2,3]), format("~t~*+~w Searching for a file in a library \c ~`.t ~d~72|~n", [3,1-3,4]), format("~t~*+~w New Built-in Predicates ~`.t ~d~72|~n", [3,1-4,5]), format("~t~*+~w write_canonical (?Term) ~`.t ~d~72|~n", [7,1-4-1,5]), format("~*+.~n~*+.~n~*+.~n", [20,20,20]), format("~t~*+~w File Specifications ~`.t ~d~72|~n", [3,1-7,17]), format("~t~*+~w multifile(+PredSpec) ~`.t ~d~72|~n", [7,1-7-1,18]). | ^D % user compiled, 20.783 sec 4888 bytes yes | ?- toc(1.5). Table of Contents 1. Documentation supplement for Quintus Prolog Release 1.5 ........... 2 1-1 Definition of the term "loaded" ............................... 2 1-2 Finding all solutions ......................................... 3 1-3 Searching for a file in a library ............................. 4 1-4 New Built-in Predicates ....................................... 5 1-4-1 write_canonical (?Term) ................................. 5 . . . 1-7 File Specifications .......................................... 17 1-7-1 multifile(+PredSpec) ................................... 18 yes
2. Misc. examples:
| ?- X=12, format('X =:= ~2d', X).% These three
| ?- X=12, format("X=:= ~2d", X).% have the
| ?- X=12, format('X =:= ~*d', [2,X]).% same results
| ?- format('~s', ["string"]).% These two have
| ?- format('string', []).% the same results
| ?- X=12, Y= 123, format('X = ~d, Y = ~d', [X,Y]).
write_canonical/[1,2]
, print/[1,2]
, write/[1,2]
ref-iou