Smart printing of integers in fortran90

你说的曾经没有我的故事 提交于 2020-01-03 11:25:12

问题


I'm 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 the 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 do I allocate the correct amount of space for integer printing when I do not know how many digits are in the integer?

Update: If your compiler is F95-compliant, you can use the I0 edit descriptor (i.e., put '(A, I0, A)' for the second argument of the write function in my example above. Thanks @janneb!


回答1:


Use the I0 edit descriptor. Well, to be pedantic IIRC that is Fortran 95, so if you're really strict about no more than F90, then I suppose this won't work.



来源:https://stackoverflow.com/questions/4760600/smart-printing-of-integers-in-fortran90

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!