I would like to output real variables in a formatted file. If the variables are non-zero, format statements are used. But if variables are zero, then only blank spaces are outputted, similar to what Iw.0 does. Is it possible to do this in the format statements? Thank you.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
No, not with a format statement, but this is reasonably easy to do by writing the values to a string and processing. Below is a demo. Probably better to put into a subroutine.
program demo
real, dimension (6) :: values = [ 1.0, 2.0, 0.0, 4.0, 0.0, 6.0 ]
character (len=100) :: string
integer :: pos
write (string,'( 6 (1X, F4.1 ) )' ) values
write (55, '(A)' ) trim (string)
MakeBlanks: do
pos = index (string, "0.0")
if ( pos < 1 ) exit MakeBlanks
string (pos:pos+2) = " "
end do MakeBlanks
write (55, '(A)' ) trim (string)
end program demo