next up previous

12.4.2.6 Format

The format function allows a user to send formatted output to a device attached to a logical name. It can be used in place of printout when special formatting of output information is desired. Although a slightly more complicated function, format provides much better control over how the output is formatted. The format commands are similar to the printf statement in C. The format function always returns a string containing the formatted output. A logical name of may be used when the formatted return string is desired without writing to a device.

Syntax

(format <logical-name> <string-expression> <expression>*)

If is given, output is sent to . The second argument to format, called the control string, specifies how the output should be formatted. Subsequent arguments to format (the parameter list for the control string) are the expressions which are to be output as indicated by the control string. Format currently does not allow expressions returning multifield values to be included in the parameter list.

The control string consists of text and format flags. Text is output exactly as specified, and format flags describe how each parameter in the parameter list is to be formatted. The first format flag corresponds to the first value in the parameter list, the second flag corresponds to the second value, etc. The format flags must be preceded by a percent sign (%) and are of the general format

%M.Nx

where x is one of the flags listed below, the minus sign is an optional justification flag, and M and N are optional parameters which specify the field width and number of digits following the decimal place. If M is used, at least M characters will be output. If more than M characters are required to display the value, format expands the field as needed. If M starts with a 0 (e.g., %07d), a zero is used as the pad character; otherwise, spaces are used. If N is not specified, it defaults to six digits for floatingpoint numbers. If a minus sign is included before the M, the value will be left justified; otherwise the value is right justified.

Format Flags

d Display parameter as a long integer. (The N specifier has no meaning.)

f Display parameter as a floating-point number.

e Display parameter as a floating-point using power of 10 notation.

g Display parameter in the most general format, whichever is shorter.

o Display parameter as an unsigned octal number. (The N specifier has no meaning.)

x Display parameter as an unsigned hexadecimal number. (The N specifier has no meaning.)

s Display parameter as a string. Strings will have the leading and trailing quotes stripped. (The N specifier has no meaning. Zero also cannot be used for the pad character.)

n Put a new line in the output.

r Put a carriage return in the output.

% Put the percent character into the output.

Example

CLIPS> (format t "Hello World!% n")
Hello World!
"Hello World!
"
CLIPS> (format nil "Integer:     |% ld|" 12)
"Integer:     |12|"
CLIPS> (format nil "Integer:     |% 4ld|" 12)
"Integer:     |  12|"
CLIPS> (format nil "Integer:     |% -04ld|" 12)
"Integer:     |12  |"
CLIPS> (format nil "Float:       |% f|" 12.01)
"Float:       |12.010000|"
CLIPS> (format nil "Float:       |% 7.2f| "12.01)
"Float:       |  12.01| "
CLIPS> (format nil "Test:        |% e|" 12.01)
"Test:        |1.201000e+01|"
CLIPS> (format nil "Test:        |%7.2e|" 12.01)
"Test:        |1.20e+01|"
CLIPS> (format nil "General:     |% g|" 1234567890)
"General:     |1.23457e+09|"
CLIPS> (format nil "Hexadecimal: |% x|" 12)
"Hexadecimal: |c|"
CLIPS> (format nil "Octal:       |% o|" 12)
"Octal:       |14|"
CLIPS> (format nil "Symbols:     |% s| |% s|" value-a1 capacity)
"Symbols:     |value-a1| |capacity|"
CLIPS>


Portability Note

The format function uses the C function sprintf as a base. Some systems may not support sprintf or may not support all of these features, which may affect how format works.


next up previous