TExcelTAD
Home › Fortran › Fortran Format Descriptors

Fortran Format Descriptors

A format is a string in parentheses that describes, item by item, how to turn values into text. Each edit descriptor handles one value (or adds spacing/text). Formats make output identical on every compiler, so this course uses them from now on.

The main descriptors

Descriptor For Meaning Example → text
iw integer width w, right-aligned i5: 42 → 42
i0 integer minimal width i0: 42 → 42
iw.m integer at least m digits (zero-padded) i5.3: 7 → 007
fw.d real fixed point, d decimals f8.3: π → 3.142
f0.d real minimal width f0.2: 2.5 → 2.50
esw.d real scientific, 1 digit before the point es10.3: 12345.0 → 1.234E+04
ew.d real exponential, 0. before digits e10.3: 12345.0 → 0.123E+05
a / aw character the string / in width w a: 'hi' → hi
lw logical T or F right-aligned l2: .true. → T
g0 any general, minimal width
nx — n blanks 3x
'text' — literal text 'x = '
/ — start a new line
fortran · try it yourself
program descriptors
  implicit none
  integer :: n = 42
  real :: x = 3.14159265

  print '(a, i5, a)',  '[', n, ']'
  print '(a, i0, a)',  '[', n, ']'
  print '(a, i5.4, a)', '[', n, ']'
  print '(a, f8.3, a)', '[', x, ']'
  print '(a, f0.1, a)', '[', x, ']'
  print '(a, es12.4, a)', '[', x * 1000, ']'
  print '(a, l3, a)', '[', n > 40, ']'
end program descriptors
[   42]
[42]
[ 0042]
[   3.142]
[3.1]
[  3.1416E+03]
[  T]

Real values are rounded, not truncated, to the number of decimals you ask for.

Building a table

Widths line values up in columns. Text can be included directly in the format, and nx adds spaces:

fortran · try it yourself
program table
  implicit none
  character(len=8) :: planet(4) = [character(len=8) :: 'Mercury', 'Venus', 'Earth', 'Mars']
  real :: distance(4) = [0.387, 0.723, 1.000, 1.524]     ! astronomical units
  real :: period(4)
  integer :: i

  period = distance ** 1.5        ! Kepler's third law, in years
  print '(a8, 2x, a8, 2x, a8)', 'Planet', 'a (AU)', 'T (yr)'
  print '(a)', repeat('-', 28)
  do i = 1, 4
    print '(a8, 2x, f8.3, 2x, f8.3)', planet(i), distance(i), period(i)
  end do
end program table
  Planet    a (AU)    T (yr)
----------------------------
Mercury      0.387     0.241
Venus        0.723     0.615
Earth        1.000     1.000
Mars         1.524     1.881

Notice that a8 right-aligns a shorter string in the header ('Planet' gets two leading blanks), but a variable declared character(len=8) already contains trailing blanks, so it appears left-aligned.

When a value doesn't fit

If a number needs more characters than the width allows, Fortran prints asterisks instead of a wrong number. This is a clear signal to widen the field:

fortran · try it yourself
program overflow
  implicit none
  print '(i3)', 12345
  print '(f5.2)', 1234.5
  print '(f0.2)', 1234.5        ! minimal width always fits
end program overflow
***
*****
1234.50

Repeat counts and groups

A number before a descriptor repeats it: 3i4 = i4, i4, i4. Parentheses group several descriptors: 2(a, i3). *( … ) repeats a group as often as needed.

fortran · try it yourself
program repeats
  implicit none
  integer :: scores(5) = [88, 92, 79, 95, 60]

  print '(5i4)', scores
  print '(2(a, i0, 2x))', 'min=', minval(scores), 'max=', maxval(scores)
  print '(a, *(i0, :, ", "))', 'scores: ', scores
end program repeats
  88  92  79  95  60
min=60  max=95
scores: 88, 92, 79, 95, 60

The colon : in the last format stops processing as soon as there are no values left, so no comma is printed after the final score.

Exponent forms and g0

fortran · try it yourself
program exponents
  use iso_fortran_env, only: real64
  implicit none
  real(real64) :: avogadro = 6.02214076e23_real64
  real(real64) :: tiny_val = 1.6e-300_real64

  print '(es12.4)', avogadro
  print '(e12.4)', avogadro
  print '(en12.3)', 47000.0_real64      ! engineering: exponent multiple of 3
  print '(es12.4e3)', tiny_val           ! 3-digit exponent field
  print '(g0)', avogadro
  print '(3(g0, 1x))', 7, 2.5, 'text'
end program exponents
  6.0221E+23
  0.6022E+24
  47.000E+03
 1.6000E-300
0.60221407599999999E+24
7 2.50000000 text

g0 chooses a sensible representation for any type, but for reals the result can be long; prefer f or es when you know the magnitude.

Formats in a variable

The format is just a character string, so it can be stored in a constant and reused:

fortran
character(len=*), parameter :: row_fmt = '(a10, 3f10.4)'
write(*, row_fmt) name, x, y, z

Test yourself

What does print '(f7.2)', 3.14159 output?

  1. 3.14
  2. 3.14
  3. 3.14159
  4. 3.142

What does Fortran print when a number does not fit its field width?

  1. The number truncated
  2. Asterisks
  3. A runtime error
  4. The number in a wider field

Which descriptor prints an integer with the minimal width?

  1. i
  2. i0
  3. i*
  4. a