[Next] [Up] [Previous]

The FORMAT Statement

A FORMAT statement has the form

       FORMAT (default-control-character; format-code, format-code...\
               default-control-character; format-code, format-code...)

where / can replace \, and # is equivalent to \'+';`.

The backslash indicates end of record, allowing a FORMAT statement to call for the generation of more than one record.

Also, parentheses can enclose a group of format codes, to be preceded by a number giving the number of times that that group of codes is to be repeated. Repeating a single code is done by simply preceding it with the number of repeats desired.

Available format codes, and their meanings, are:

any character string enclosed in quotes print this string on output; ignore the characters to which the string corresponds on input. (If the same FORMAT statement is used for both input and output, the string itself is changed to contain what was in the characters input previously in those positions, as becomes apparent only on output.) Unlike FORTRAN, double quotes may also be used here.
nH followed by n characters same as above. Note that, unlike in FORTRAN, while this is acceptable as a format code, it is NOT a legal character-valued constant. (use %H(n) instead)
X print one blank on output (unchangeably), ignore one space on input.
Tn print blanks, or ignore spaces, until position n is reached.
TLn move backwards n spaces
TRn move forwards n spaces (same as nX)
SP print leading plus sign for positive numbers
SS only print -, but blank for +
S same as SS (the default)
BN ignore trailing blanks on fixed-field input
BZ trailing blanks, on fixed-field input, considered zeroes, wherever that is the default (not including Kn, for example)
ES print numbers in E format in the form -n.nnnnnE+nn (the default, similar to conventional scientific notation)
EZ print numbers in E format in the form -0.nnnnnE+nn (as done by FORTRAN)
EL print numbers in E format in the form -n.nnnnn#+nn
EF print numbers in E format in the same form as ES, but with D or Q replacing E if the number being printed was double or quadruple precision, respectively (useful for the automatic generation of FORTRAN programs)

Note that neither of the above format codes corresponds to an item in the parameter list of the input-output statement. The remaining codes do each correspond to an item, except that an array-valued item takes one code for each element, and a COMPLEX item takes two codes, and a QUATERNION item four.

In print, or read, an integer in the next n spaces of the record being output or input. Trailing blanks, on input, are interpreted as zeroes.
Kn as In, but ignore trailing blanks on input.
Jn as In, but fill with leading zeroes on output.
Y print an integer using however many spaces are needed, and print one space; on input, read in an integer until a delimiter (such as a space or a comma) is reached.
Fw.d print, or read, in the next w spaces, a floating-point number, with d digits after the decimal point. The decimal point is printed, but, on input, is assumed if absent. The form Fw is equivalent to Fw.0 and allowed for both input and output, and results in printing a decimal point at the end of the number on output.
Ew.d print, or read, a floating-point number in exponential notation, with d significant figures. On input, d may be omitted.
Gw.d attempt output using format Fw.d; if this fails, use Ew.d format instead. Accept either format on input, except that no decimal point is assumed.
Sw print a floating-point number in w spaces with as many significant figures as possible.
Vd print a floating-point number with d significant figures in as many spaces as necessary.
Ww.d print, or read, in Fw.d format, an INTEGER parameter; dividing by 10 to the d-th power before output, or multiplying by 10 to the d-th power after input if a decimal point is present in the input text.
Uw.d similar to Ww.d, except that multiplication on input is always performed (do not, essentially, assume a decimal point in input).
Ln prints T for a LOGICAL variable with the value _T, and F otherwise, as the last character in a field otherwise filled with blanks.
An print the text of a CHARACTER, STRING, or FIXED_STRING variable in n spaces, padding with blanks on the right if necessary; for other variables, handle their contents without conversion as if they contained text.
Rn as An, except padding (or ignoring part of the field) is done on the left.
Zn as An, except that each character, or machine word, is converted to (on output) or from (on input) the appropriate number of hexadecimal digits.
On as Zn, except that octal digits are used, and word boundaries are preferred over character boundaries.
M print the entire contents of an array, or a MUTABLE variable, without using any subsequent format codes (not required within an input/output statement, since each format code is linked to a specific variable); this may be followed by the format codes to use for that item within parentheses.
P'...'

print a number in the format outlined, character by character, in the string ...`. Double quotes, or an H format code, may be used to outline that string as well.

In the first part of the string:

9 indicates a position that may be filled by a leading or trailing zero
Z indicates a position that may be filled by a digit that is not a leading or trailing zero (as well as a floating sign or $ sign, or a background character)
. indicates the position of the decimal point
P indicates the position of the sign that may be filled by + or -
S indicates the position of the sign that may be filled by -
, indicates a character that is printed if it is preceded and followed by digits the printing of which has not been suppressed by reason of being a leading or trailing zero
# indicates a character just printed as is, as does a blank and a zero
$ preceding the description of a number indicates an $ sign to be printed: either where it is placed, if no leading zeroes are suppressed, or in the digit or comma position immediately preceding the first printed digit of the number otherwise. After the number, it moves to the left if trailing zeroes after the decimal point are suppressed, in the same way.
+ indicates a + sign if the number is positive, and a - sign if the number is negative, to be printed in the same way as $
- indicates a - sign if the number is negative, to be printed the same way as $; if the number is positive, it generates a blank that does not move.
E indicates the point at which the 'mantissa' portion of a number ends, and the 'exponent' portion begins, each to be considered as a separate number
N acts like P; however, after an E, this indicates the sign of the number rather than the sign of the exponent
X indicates a position where a character from a character string is to be printed (left-justified)
C indicates a position where a character from a character string is to be printed (centred)
R indicates a position where a character from a character string is to be printed (right-justified)
The second part of the string acts as a delimiter for the first part of the string, and has one of two possible forms:
/ indicates the end of the first part of the string, and the beginnning of the third through seventh parts of the string.
(n)

serves the function of /, but in addition indicates that the last n digits of the number as printed (including trailing zeroes suppressed) are to be digits after the actual decimal point of the number to be printed, regardless of where or whether a decimal point is to be included in the printed number. This serves a function similar to the V character in COBOL picture clauses; placing it in the second part of the FALCON picture clause allows strict character-for-character correspondence to be maintained in the first part of the FALCON picture clause.

Note also that n may have a minus sign preceding it. This does not cause the number to be printed to be multiplied by 10 to the n-th power; rather, the amount of scaling is determined by the difference between n and the number of digits shown as being after the decimal point by the format chosen (or the number of printed zeroes appended to the number).

The third through seventh parts of the string are not delimited; instead, each of them must contain a specific number of characters as determined from the first part of the string, or they can be omitted by providing a shorter string in the P format code.

The third part of the string contains as many characters as the first part of the string, and contains characters to be printed instead of:

  • the characters ".", "#", " ", "0", and "E", which are otherwise printed when they ocurr in the first part of the string
  • a - sign printed in response to a P, S, or N character in the first part of the string
  • a blank generated by the suppression of leading or trailing zeroes, including a space left behind by an $, +, or - sign, and a comma not printed because of suppression of zeroes adjacent to it

The fourth part of the string contains one character corresponding to each P or N character in the first part of the string, and these characters are to be printed instead of:

a + sign printed in response to a P or N character in the first part of the string

The fifth part of the string contains one character for every $, +, or - character in the first part of the string, and contains the character to print instead of:

the $ sign printed in response to the corresponding $ sign in the first part of the string, or any - signs printed in response to the corresponding + or - character in the first part of the string, where the +, -, and $ characters are all regarded as a single block that moves along the string

The sixth part of the string contains one character for every + sign in the first part of the string, and none otherwise, and contains the character to be printed instead of:

a + sign printed in response to the corresponding + sign in the first part of the string

The seventh part of the string contains one character for every , character in the first part of the string, and contains the character to print instead of:

a , printed in response to the corresponding , in the first part of the string (where comma printing is not suppressed because of zero suppression)

It is possible to omit the last part of the string, or that one and the one before it, and so on, simply by ending the string before all parts have been written out. In addition, the seventh part of the string may be only one character long, in which case that character replaces the , characters generated by all the commas in the first part of the string.

Thus, if you want to print numbers with two digits following the decimal point, always printed, and with CR appearing at the end if they are positive, DB if negative, with leading zeroes suppressed, a $ sign immediately preceding the first digit of the number, and asterisks filling the positions before that, having commas separating the thousands, and having a value up to 9,999,999.99, you can use the format code

     P'$Z,ZZZ,ZZ9.99PP/*********n.nnDBCR$'

where lowercase letters fill the "Don't Care" positions of the code to help keep track of position.

On the other hand, if you want to print a + or - sign in front of the number and the dollar sign, and want to use . to mark off thousands and , for the decimal point, European style, use the format code

     P'$+Z,ZZZ,ZZ9.99/*********n,nn$+-.'

Multiple dollar signs in the first part of the string move as a block to precede the number; so, if you want to talk about Canadian dollars, you can make that clear with

     P'$$$$$$Z,ZZZ,ZZ9.99PP/**************n.nnDBCR$(CAN)'

(actually, $(Cdn.) is preferable, but that form was not used in this example so that lower-case letters could still be used to indicate irrelevant character positions.)

In addition, there are two items that modify other format specifications:

nP (preceding the specification) the printed form of the number is to be 10 to the n-th power times the actual number in the variable undergoing input or output. The number n may be negative. No ambiguity results from this use of the letter P, as it is meaningless to scale a character string.
En (following the specification) specifies the number of digits in the exponent for Gw.d and Ew.d

Note that the format specification does not specify whether the number being printed is double precision or real, BCD or integer; thus, type information is taken from the PRINT, WRITE, READ, or INPUT statement. (The Q format code for REAL*16 numbers, for example, is not supported.)

Also, as their respective names imply, the control character in an input/output statement takes precedence over the default control character in any FORMAT statement it references.


[Next] [Up] [Previous]