Special sequences start with the tilde (
),
followed by an optional numeric argument, optionally followed by a colon
modifier (:),
146The colon modifiers is a
SWI-Prolog extension, proposed by Richard O'Keefe. followed
by a character describing the action to be undertaken. A numeric
argument is either a sequence of digits, representing a positive decimal
number, a sequence ~
‘<character>
,
representing the character code value of the character (only useful for
~t
) or a asterisk (
), in which
case the numeric argument is taken from the next argument of the
argument list, which should be a positive integer. E.g., the following
three examples all pass 46 (*
) to .
~t
:
?- format('~w ~46t ~w~72|~n', ['Title', 'Page']). ?- format('~w ~`.t ~w~72|~n', ['Title', 'Page']). ?- format('~w ~*t ~w~72|~n', ['Title', 46, 'Page']).
Some format expressions may call back Prolog, i.e., ~p
,
~W
, ~@
and user defined extensions registered
with
format_predicate/2.
Output written to the stream current_output
is merged into
the format/2
output. If there is no pending
rubber (~t
) and the the position notation aligns,
only the output is switched. Otherwise the output is captured in a
temporary memory buffer and emitted after the callback finishes. The
system attempts to preserve the position and alignment promises. It sets
the tty
property of the temporary stream to reflect the
main stream and uses the position information of the temporary stream to
update its notion of the position. Notable ansi_format/3
cooperates properly in callbacks.147As
of version 8.3.30.
Numeric conversion (d
, D
, e
, E
, f
,
g
, G
, h
and H
)
accept an arithmetic expression as argument. This is introduced to
handle rational numbers transparently (see section
4.27.2.2). The floating point conversions allow for unlimited
precision for printing rational numbers in decimal form. E.g., the
following will write as many 3's as you want by changing the‘50’.
?- format('~50f', [10 rdiv 3]). 3.33333333333333333333333333333333333333333333333333
~
a
c
d
The colon modifier (e.g., ~:d
) causes the number to be
printed according to the locale of the output stream. See section
4.23.
D
~:d
,
but using the fixed English locale. If D is modified using the
colon (~:D
), it uses the Prolog grouping character _
.
See also setup_prolog_integer_grouping/0.
Future versions may also support line breaks in big integers.
e
%.<precision>e
.
E
f
g
G
h
H
~f
). If the argument is -1, the number
is always written using exponential notation. Otherwise, number is
written using exponential notation if the exponent is less than Arg-1
or greater than Arg+d, where d is the
number of digits emitted to establish the required precision. Using an
argument larger than the the maximum exponent such as ~999h
never uses exponential notation. The default argument is 3. The
predicate write/1
and friends act as if using the format ~3h
. This option is
compatible to SICStus Prolog.
i
I
_
). The argument describes the size of each digit group.
The default is 3. See also section
2.15.1.5. For example:
?- A is 1<<100, format('~10I', [A]). 1_2676506002_2822940149_6703205376
k
n
N
p
q
r
~16r
prints its argument hexadecimal. The argument should
be in the range [2, ... , 36]. Lowercase letters are used for
digits above 9. The colon modifier may be used to form locale-specific
digit groups.
R
s
@
current_output
stream is inserted at this place. Goal
is called in the module calling format/3.
This option is not present in the original definition by Quintus, but
supported by some other Prolog systems. The goal is executed as \+ \+ Goal
,
i.e., bindings created by the goal are discarded.
t
~t
statements between the tab stops. This space is padded
with spaces by default. If an argument is supplied, it is taken to be
the character code of the character used for padding. This can be used
to do left or right alignment, centering, distributing, etc. See also ~|
and ~+
to set tab stops. A tab stop is assumed at the start
of each line.|
~t
’s to be distributed between the previous and this
tab stop.
If the current column is at or past the requested tabstop and the
modifier (:) is used, a newline is inserted and the padding character of
the last ~t
is used to pad to the requested position.
+
~|
) relative to the last tab stop or the
beginning of the line if no tab stops are set before the ~+
.
This constructs can be used to fill fields. The partial format sequence
below prints an integer right-aligned and padded with zeros in 6
columns. The ... sequences in the example illustrate that the integer is
aligned in 6 columns regardless of the remainder of the format
specification.
format('...~|~`0t~d~6+...', [..., Integer, ...])
w
W
format('~W', [Term, [numbervars(true)]])
. This option is
SWI-Prolog specific.Example:
simple_statistics :- <obtain statistics> % left to the user format('~tStatistics~t~72|~n~n'), format('Runtime: ~`.t ~2f~34| Inferences: ~`.t ~D~72|~n', [RunT, Inf]), ....
will output
Statistics Runtime: .................. 3.45 Inferences: .......... 60,345