Is it possible to output a variable with zero valu

2019-03-02 15:42发布

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条回答
三岁会撩人
2楼-- · 2019-03-02 16:43

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
查看更多
登录 后发表回答