FORTRAN 90 Lecture : Rafel Hekmat Hameed University of Babylon Subject : Fortran 90 College of Engineering Mechanical Engineering Dep. Year : Second B.Sc FORMAT Statement The format statement may be used to read or write data in a form other than the default format. A format statement is a labeled statement and may be used by a WRITE or READ statement within the same program unit by specifying the label number as the second parameter, for example: READ (*,100) i, j WRITE (*,100) i, j WRITE (*,200) x, y ..... 100 FORMAT (2I) 200 FORMAT (2F10.6) The general form of a FORMAT statement is label FORMAT (flist) where flist is a list of edit descriptors which include I,F,E,ES,EN,D,G,L,A,H,T,TL,TR,X,P,BN,BZ,SP,SS,S,/,:,',(,) only the following will be covered I,F,E,ES,EN,A,X,/,:,',(,) The labeled FORMAT statement may be replaced by specifying the format descriptor list as a character string directly in the WRITE or READ statement, as follows: READ (*,'(2I)') I, J WRITE (*,'(2F12.6)') X, Y ϭ INTEGER The edit descriptor I is used to control the format of integers and can have the form Iw or aIw For example: INTEGER :: itest=1234567 !number to write ... WRITE(*,*) itest ! 1234567 WRITE(*,’(I6)’) itest !****** WRITE(*,’(I10)’) itest ! 1234567 WRITE(*,10) itest 10 format (I10) 1 2 3 4 5 6 7 4 5 6 7 1 2 3 WRITE(*,’(2I7)’) itest 1 2 3 4 5 6 7 WRITE(*,10) itest 10 format ("itest=", I10) Integer ::A=44 , b=-50 WRITE(*,100) a,b 100 format (2I3) 4 4 - 5 0 Real – Fixed Point Form The Fw.d descriptor is for REAL output. F is for REAL w is the width of field, which indicates that a real number should be printed with w positions. d indicates the number of digits after the decimal point. This is shown in the figure below: REAL :: itest=123.4567 WRITE(*,’(F10.4)’) itest 1 Ϯ 2 3 . 4 5 6 7 Consider the following example: REAL :: a = 12.34, b = -0.945, c = 100.0 WRITE(*,"(3F6.2)") a, b, c Real - Exponential Form The edit descriptor E is used to control the format of real numbers where a floating decimal point notation is required. It has the form Ew.d The Ew.d descriptor generates real numbers in the following form: Of these w positions, the last three are for the exponent, including its sign, the middle d positions are for the number in normalized form. The Ew.dEe descriptor generates real numbers in the following form: Examples Character The A edit descriptor is used to control the format of characters and strings. It has the form A or Aw. The A descriptor will writes as many characters as required while Aw writes a string of width w. If w is bigger than ϯ the character string leading spaces are added before the string’s characters. For example: CHARACTER(LEN=8) :: long=’Bookshop’ CHARACTER(LEN=1) :: short=’B’ ... WRITE(*,*) long ! Bookshop WRITE(*,’(A)’) long !Bookshop WRITE(*,’(A8)’) long b o o k s h o p b o o k s b o o k s h b b o o WRITE(*,’(A5)’) long WRITE(*,’(A10)’) long o p WRITE(*,’(A)’) short b WRITE(*,’(2A3) short, long The L Descriptor The Lw descriptor is for LOGICAL output. While Fortran uses .TRUE. and .FALSE. to indicate logical values true and false, respectively, the output only show T and F. The general form of this descriptor is as follows: rLw The meaning of r and w are: ϰ L is for LOGICAL w is the width of field, field, which indicates that a logical value should be printed with w positions. The output of a LOGICAL value is either T for .TRUE. or F for .FALSE. r is the repetition indicator, indicator, which gives the number of times the edit descriptor should be repeated Examples Let us look at the following example. There are two LOGICAL variables a and b with values .TRUE. and .FALSE.,, respectively. In the following table, the WRITE statements are shown in the left and their corresponding output are shown in the right. ϱ