How to get rid of unwanted spacing in Fortran print output?

This may seem like a trivial problem, but I haven't been able to find an answer via googling. I have this little program:

Program Test_spacing_print
  Integer:: N
  Real:: A,B

  N=4; A=1.0; B=100.0

  print*,'N =',N

  print*,'A =',A,' B =',B
  print '(2(A3,F8.2,1X))' ,'A =',A,' B =',B
  print 20, A,B
  20 format('A =',F8.2,x,'B =',F8.2)

End Program Test_spacing_print

      

which gives me the output:

     N =           4
 A =   1.00000000      B =   100.000000
A =    1.00  B   100.00
A =    1.00 B =  100.00

      

I want to get rid of the unwanted space I am getting after the sign =

, that is, my desired output should look like (1 space after =

):

 N = 4
 A = 1.00000000 B = 100.000000
 A = 1.00 B = 100.00
 A = 1.00 B = 100.00

      

Is this possible in fortran?

+3


source to share


1 answer


You say you have "unwanted" space in the exit, but you have exactly the space you requested with the explicit formats specified. If you didn't specify a format, list-oriented output means you can't talk about distance.

For output A

, you have an edit handle F8.2

: the field width will be 8. With two digits after the decimal point and the decimal point itself, which leaves you with five digits for the digits (and sign) up to the decimal point. Thus, for a A

value 1.

without additional sign printing, you have four spaces.

Since Fortran 95 introduced an edit handle I0

, so it allows F0.d

. [And for other descriptors, although it G0.d

was added even later.] F0.2

Will provide the minimum field width with those two digits after the decimal point, which is what you want. Note, however, that you will need to explicitly add a space after the sign =

:



print '("N = ", I0)', N
print '(2(A4,F0.2,:,1X))' ,'A = ',A,'B = ',B

      

[I also used an edit handle :

to avoid trailing whitespace.]

If you want to truly answer Fortran 90 as you noted, then it won't be that good, but it can still be done.

+2


source







All Articles