next up previous
Next: Programming Format Up: Formatted Write Previous: Writef

Format

format( +Format)

  Defined as `format(Format) :- format(Format, []).'
format( +Format, +Arguments)

  Format is an atom, list of ASCII values, or a Prolog string. Arguments provides the arguments required by the format specification. If only one argument is required and this is not a list of ASCII values the argument need not be put in a list. Otherwise the arguments are put in a list.

Special sequences start with the tilde (~), followed by an optional numeric argument, 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 ASCII value of the character (only useful for ~t) or a asterisk (*), in when the numeric argument is taken from the next argument of the argument list, which should be a positive integer. Actions are:

-1ex
Output the tilde itself.
a
Output the next argument, which should be an atom. This option is equivalent to w. Compatibility reasons only.
c
Output the next argument as an ASCII value. This argument should be an integer in the range [0, ..., 255] (including 0 and 255).
d
Output next argument as a decimal number. It should be an integer. If a numeric argument is specified a dot is inserted argument positions from the right (useful for doing fixed point arithmetic with integers, such as handling amounts of money).
D
Same as d, but makes large values easier to read by inserting a comma every three digits left to the dot or right.
e
Output next argument as a floating point number in exponentional notation. The numeric argument specifies the precission. Default is 6 digits. Exact representation depends on the C library function printf(). This function is invoked with the format %.<percission>e.
E
Equivalent to e, but outputs a capital E to indicate the exponent.
f
Floating point in non-exponentional notation. See C library function printf().
g
Floating point in e or f notation, whichever is shorter.
G
Floating point in E or f notation, whichever is shorter.
i
Ignore next argument of the argument list. Produces no output.
k
Give the next argument to displayq/1 (canonical write).
n
Output a newline character.
N
Only output a newline if the last character output on this stream was not a newline. Not properly implemented yet.
p
Give the next argument to print/1.
q
Give the next argument to writeq/1.
r
Print integer in radix the numeric argument notation. Thus ~16r prints its argument hexadecimal. The argument should be in the range [2, ... 36]. Lower case letters are used for digits above 9.
R
Same as r, but uses upper case letters for digits above 9.
s
Output a string of ASCII characters from the next argument.
t
All remaining space between 2 tabstops is distributed equaly over ~t statements between the tabstops. This space is padded with spaces by default. If an argument is supplied this is taken to be the ASCII value 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 tabstop is assumed at the start of each line.
Set a tabstop on the current position. If an argument is supplied set a tabstop on the position of that argument. This will cause all ~t's to be distributed between the previous and this tabstop.
+
Set a tabstop relative to the current position. Further the same as ~|.
w
Give the next argument to write/1.

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
sformat( -String, +Format, +Arguments)

  Equivalent to format/3, but ``writes'' the result on String instead of the current output stream. Example:
?- sformat(S, '~w~t~15|~w', ['Hello', 'World']).

S = "Hello          World"
sformat( -String, +Format)

  Equivalent to `sformat(String, Format, []).'



next up previous
Next: Programming Format Up: Formatted Write Previous: Writef



Passani Luca
Tue Nov 14 08:58:33 MET 1995