Intelligent integer printing in fortran90

I am learning Fortran90 after a brief introduction to Fortran77 a few years ago. When printing integers in Fortran, you must specify how many spaces you want to reserve for printing an integer. Consider this program ...

implicit none

integer :: i
i = 123

write(*, '(A, I3, A)')  "'", i, "'"  !3 spaces for output = no padding
write(*, '(A, I5, A)')  "'", i, "'"  !5 is too many, so output is padded
write(*, '(A, I2, A)')  "'", i, "'"  !2 is too few, so output is jibberish
write(*, '(A, I:, A)')  "'", i, "'"  !Default behavior

end program

... which generates the following output.

'123'
'  123'
'**'
'         123'

How to assign the correct amount of space for integer printing when I do not know how many digits are in the integer?

Refresh . If your compiler is F95 compatible, you can use the edit descriptor I0(i.e. put the '(A, I0, A)'functions for the second argument writein my example above Thanks @janneb!

+5
source share
1

I0. , IIRC, Fortran 95, , , F90, , .

+8

All Articles