next up previous contents index
Next: Functions Up: Basic Fortran Concepts Previous: DO Loops

Formatted Output

The table of values would have a better appearance if the decimal points were properly aligned and if there were only two digits after them. The last figure in the table is actually less than a thirtieth of a penny, which is effectively zero to within the accuracy of the machine. A better layout can be produced easily enough by using an explicit format specification instead of the list-directed output used up to now. To do this, the last WRITE statement in the program should be replaced with one like this:
WRITE(UNIT=*, FMT='(1X,I9,F11.2)') IYEAR, AMOUNT
The amended program will then produce a neater tabulation:

 
Enter amount, % rate, years 
2000, 9.5, 5 
Annual repayments are    520.8728     
End of Year  Balance 
        1    1669.13 
        2    1306.82 
        3     910.10 
        4     475.68 
        5        .00
The format specification has to be enclosed in parentheses and, as it is actually a character constant, in a pair of apostrophes as well. The first item in the format list, 1X, is needed to cope with the carriage-control convention: it provides an additional blank at the start of each line which is later removed by the Fortran system. There is no logical explanation for this: it is there for compatibility with very early Fortran system. The remaining items specify the layout of each number: I10 specifies that the first number, an integer, should be occupy a field 10 columns wide; similarly F11.2 puts the second number, a real (floating-point) value, into a field 11 characters wide with exactly 2 digits after the decimal point. Numbers are always right-justified in each field. The field widths in this example have been chosen so that the columns of figures line up satisfactorily with the headings.



Mario Storti
Wed Nov 4 19:32:56 ART 1998