Format sentence of Fortran

Fortran Courses in Fortran Wiki is the place to start online, but formatting I/o is a language within a language, and if you have a more specific question do ask.

1 Like

My reference is chapter 11 â€śEdit descriptorsâ€ť of Modern Fortran Explained: Incorporating Fortran 2018

1 Like

Here is another online resource: FORMAT
Mind you: an explanation of the â€śwordsâ€ť of this little language is not the same as a â€śgrammarâ€ť or an â€śidiomâ€ť. As @NormanKirkby indicated, you may be better off to ask about specific problems that you need to solve.

1 Like

Here is a program that illustrates some Fortran formats. The output of a print statement is given in comment lines below it.

``````program formats
implicit none
real :: x(3) = [4.0,9.0,16.0]
integer :: v(3) = [4,9,16]
print*,x(1) ! * gives list-directed output. It is general and
! can be used for all types, but results will differ by compiler.
! Format strings start and end with parentheses.
! The fw.d format prints a floating point number using width w
! and with d numbers after the decimal point.
print "(f6.2)",x(1)
! 4.00
! Due to format reversion, the line below will print each element of x(:)
! on a separate line. See online Dr. Fortran essay "Revert! Revert! The End (of the format) is Nigh!"
print "(f6.2)",x
!  4.00
!  9.00
! 16.00
! Print two floats on each line
print "(f6.2,f6.2)",x
!   4.00  9.00
!  16.00
! Use a "repeat edit descriptor" to do the same thing
print "(2f6.2)",x
!   4.00  9.00
!  16.00
! Use an "infinite repeat count" to print all floats on same line
print "(*(f6.2))",x
!   4.00  9.00 16.00
! Use minimum field width descriptor f0.d to print in the smallest width possible
! The 3x is used to put 3 spaces between the numbers.
print "(*(3x,f0.2))",x
!    4.00   9.00   16.00
! Print with comma separators. The : suppresses a comma after the final item
print "(*(f0.2,:,','))",x
! 4.00,9.00,16.00
! For integers the iw descriptor prints an integer using width w, and i0 uses minimum width.
print "(i6)",v
!      4
!      9
!     16
print "(*(1x,i0))",v
!  4 9 16
! A format such iw.w will print leading zeros to fit the width
print "(*(1x,i4.4))",1,12,123,1234,12345
! 0001 0012 0123 1234 ****
! For character variables there is the 'a' edit descriptor. aw means using width w.
! Just 'a' uses the width of the character variable.
print "(a4)","boy","girl"
!  boy
! girl
print "(a)","boy","girl"
! boy
! girl
! logical variables are printed as T or F, with the l edit desciptor.
print "(*(l3))",.true.,.false.
!   T  F
! The g (general) edit desciptor can be used to print any type. One form is g0.
print "(*(1x,g0))", x,v,"boy","girl",.true.,.false.
! 4.000000 9.000000 16.00000 4 9 16 boy girl T F
! / starts a new line
print "(*(i0,1x,i0/,a,1x,a))", 3,5,"girl","boy"
! 3 5
! girl boy
! If there is insufficient width to print a number, asterisks will be printed.
print "(f3.1)",4.2,1234567.8
! 4.2
! ***
! There are scientific notation formats such as e and es for floating point
! that can help avoid such overflow.
print "(e15.8)",4.2,1234567.8
!  0.41999998E+01
!  0.12345678E+07
print "(es15.8)",4.2,1234567.8
!  4.19999981E+00
!  1.23456775E+06
! A repeat edit descriptor can be used for a parenthesized list of edit descriptors.
print "(2(1x,2(i2.2,':'),i2.2))",8,59,59,11,59,59
!  08:59:59 11:59:59
end program formats
``````
2 Likes