The FORMAT statement provides explicit information that directs how data and characters are read on input and displayed on output.
Syntax
FORMAT ( [format-items] )Where:format-items is a comma-separated list of
[r]data-edit-descriptor, control-edit-descriptor, char-string-edit-descriptor, or [r](format-items)data-edit-descriptor is
Iw[.m] Bw[.m] Ow[.m] Zw[.m] Fw.d Dw.d Ew.d[Ee]] ENw.d[Ee]] ESw.d[Ee]] Gw.d[Ee]] Lw A[w]w, m, d, and e are INTEGER literal constants that represent
field width, digits, digits after the decimal point, and exponent digits, respectively.control-edit-descriptor is
Tn TLn TRn nX S SP SS BN BZ [r]/ : kPchar-string-edit-descriptor is a CHARACTER literal constant
r, k, and n are positive INTEGER literal constants used to specify
number of repetitions of the data-edit-descriptor, char-string-edit-descriptor, control-edit-descriptor, or (format-items)
|
Edit Descriptor |
Interpretation |
Intrinsic type |
|---|---|---|
|
Iw[.m] |
ordinal number with field width of w, displays m digits |
INTEGER |
|
Bw[.m] |
binary number with field width of w, displays m digits |
INTEGER |
|
Ow[.m] |
octal number with field width of w, displays m digits |
INTEGER |
|
Zw[.m] |
hexadecimal number with field width of w, displays m digits |
INTEGER |
|
Fw.d |
decimal number with field width of w, displays d decimal places, no exponent |
REAL or COMPLEX |
|
Ew.d[Ee]] and Dw.d[Ee]] |
decimal number with field width of w, displays d decimal places, and an exponent with e digits |
REAL or COMPLEX |
|
ENw.d[Ee]] |
decimal number with field width of w, displays d decimal places, and an exponent with e digits (engineering notation) |
REAL or COMPLEX |
|
ESw.d[Ee]] |
decimal number with field width of w, displays d decimal places, and an exponent with e digits (scientific notation) |
REAL or COMPLEX |
|
Gw.d[Ee]] |
(generalized) field width of w, displays d decimal places, and an exponent with e digits |
Any intrinsic type |
|
Lw |
T or F with a field width of w |
LOGICAL |
|
A[w] |
alphanumeric with a field width of w |
CHARACTER |
|
Tn |
move n spaces from the start of the record |
None |
|
TLn |
move n spaces left of current position |
None |
|
TRn |
move n spaces right of current position |
None |
|
nX |
move n spaces right of current position |
None |
|
S |
default generation of plus sign on subsequent output |
Numeric |
|
SP |
force generation of plus sign on subsequent output |
Numeric |
|
SS |
no generation of plus sign for subsequent output |
Numeric |
|
BN |
ignore non-leading blanks on input of subsequent items |
Numeric |
|
BZ |
interpret non-leading blanks as zeros on input of subsequent items |
Numeric |
|
[r] / |
skip to the next record r is a repeat count |
None |
|
: |
terminates format control if there are no more items in the i/o list |
None |
|
kP |
set a scale factor of k for subsequent items |
REAL or COMPLEX |
The FORMAT statement must be labeled.
Edit descriptors may be nested within parentheses and may be preceded by a repeat factor. A parenthesized list of edit descriptors may also be preceded by a repeat factor, indicating that the entire list is to be repeated.
The comma between edit descriptors may be omitted in the following cases:
Within a CHARACTER literal constant, if a string delimiter character (either an apostrophe or quote) is to appear as a part of the string, it must appear as a consecutive pair of the delimiter characters without any blanks. Each such pair represents a single occurrence of the delimiter character.
Example 1
! numeric output editing integer :: i=-1 real :: r=1. write(*,101) i ! writes -1 write(*,102) i ! writes -0001 write(*,103) i ! writes 11111111111111111111111111111111 write(*,104) i ! writes 37777777777 write(*,105) i ! writes FFFFFFFF write(*,201) r ! writes 1000.00 write(*,202) r ! writes 0.01D+02 write(*,203) r ! writes +0.10E+1 write(*,204) r ! writes 1.00E+00 write(*,205) r ! writes 1.00E+00 101 format(I10) ! Show up to 10 digits, field width 10 102 format(I10.4) ! Always show 4 digits, field width 10 103 format(B34.32) ! Show 32 binary digits, field width 34 104 format(O13.11) ! Show 11 octal digits, field width 13 105 format(Z10.8) ! Show 8 hex digits, field width 10 201 format(3PF10.2)! 2 dec places field width 10 scale 3 202 format(-1P,D10.2) ! 2 dec places field width 10 scale -1 203 format(SP,E10.2E1)! 2 dec places, field width 10, ! 1 digit exponent, produce plus sign 204 format(SSEN10.2E2)! 2 decimal places, field width 10, ! 2 digit exponent suppress plus sign 205 format(ES10.2E2) ! 2 decimal places, field width 10, ! 2 digit exponentExample 2
! numeric input editing character(len=5) :: in_data1="11000" ! internal file character(len=10) :: in_data2=" 1 1"! internal file integer :: i real :: r complex :: q read(in_data1,101) i write(*,*) i ! writes 1100000000 read(in_data1,102) i write(*,*) i ! writes 11000 read(in_data1,103) i write(*,*) i ! writes 24 read(in_data1,104) i write(*,*) i ! writes 4806 read(in_data1,105) i write(*,*) i ! writes 69632 read(in_data1,201) r write(*,*) r ! writes 110. read(in_data1,202) r write(*,*) r ! writes 11000000. read(in_data2,202) r write(*,*) r ! writes 11000. read(in_data2,203) r write(*,*) r ! writes 100001. read(in_data2,204) q write(*,*) q ! writes (1.,1.) read(in_data2,205) q write(*,*) q ! writes (10.,100.) 101 format(BZI10) ! Interpret non leading blanks as zeros 102 format(BNI10) ! Ignore non leading blanks 103 format(B32) ! Read up to 32 binary digits 104 format(O11) ! Read up to 11 octal digits 105 format(Z8) ! Read up to 8 hexadecimal digits 201 format(F10.2) ! last two digits are right of decimal 202 format(-3PF10.0) ! Scale factor -3 203 format(BZF10.0) ! non leading blanks are zeros 204 format(2(F6.0)) ! Ignore blanks 205 format(BZ,2(F6.0))! non leading blanks are zerosExample 3
! generalized, logical and character editing integer :: i real :: r real(kind(1.d0)) :: d complex :: q logical :: l character(len=10) :: rdstr(2) character(len=10) :: in_data=" 1 1" character(len=20) :: in_str=" Howdy There, Folks!" read(in_data,301) i write(*,301) i ! writes 11 read(in_data,301) r write(*,301) r ! writes 0.11 read(in_data,301) d write(*,301) d ! writes 0.11 read(in_data,301) q write(*,301) q ! writes 0.11 0.0 read(in_str(8:8),301) l write(*,301) l ! writes T read(in_str(15:15),301) l write(*,301) l ! writes F read(in_str,301) rdstr write(*,301) rdstr ! writes Howdy There, Folks! read(in_str(8:8),401) l write(*,401) l ! writes T read(in_str(15:15),401) l write(*,401) l ! writes F read(in_str,501) rdstr write(*,501) rdstr ! writes Howdy There, Folks! write(*,501) "howdy" ! writes howdy write(*,501) '"howdy"' ! writes "howdy" write(*,501) "'howdy'" ! writes 'howdy' write(*,501) """howdy""" ! writes "howdy" write(*,501) '''howdy''' ! writes 'howdy' 301 format(2G10.2) ! general editing, field width 10 401 format(L10) ! Logical T or F, field width 10 501 format(2A10) ! Alphanumeric string, field width 10Example 4
! positional editing real :: r(3)=(/-1., 0., 1./) write(*,201) r ! writes -1.00 0.00 1.00 write(*,202) r ! writes 1.00 -1.00 0.00 write(*,203) r ! writes 1.00 0.00 -1.00 write(*,204) r ! writes -1.00 0.00 1.00 write(*,205) r ! writes -1.00 ! 0.00 ! 1.00 201 format(TR10,3F10.2) 202 format(T21,F10.2,T31,F10.2,T11,F10.2) 203 format(TR30,F10.2,2(TL20,F10.2)) 204 format(10X,3(F10.2)) 205 format(3(T21,F10.2,/))Example 5
! formats without statements integer :: i_a(3,3)=reshape((/1,2,3,4,5,6,7,8,9/), & shape(i_a)) integer :: i_b(2,3)=reshape((/1,2,3,4,5,6/),shape(i_b)) integer :: i_c(3,2)=reshape((/1,2,3,4,5,6/),shape(i_c)) call write_array2d(i_a) call write_array2d(i_b) call write_array2d(i_c) contains subroutine write_array2d(i) ! compose a format and integer :: i(:,:) ! write rank two array character(40) :: fmt ! in rows and columns write(fmt,*) "(",size(i,1),"(",size(i,2),"I10,/))" write(*,*) fmt ! write the format string write(*,fmt) i ! write array using the format string end subroutine end program