6

It may look like a trivial issue, but I couldn't find any answer through 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 that I get after = sign, i.e. 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 it possible in fortran ?

nbro
  • 15,395
  • 32
  • 113
  • 196
hbaromega
  • 2,317
  • 2
  • 24
  • 32

1 Answers1

5

You say that you have "unwanted" space in the output, but you have exactly the space that you asked for with your specified explicit formats. When you didn't provide a format, list-directed output means that you have no say on the spacing.

To output A you have the edit descriptor F8.2: the field width will be 8. With two digits after the decimal point and the decimal point itself that leaves you with five digits for the digits (and sign) before the decimal point. So, for A value 1. without the optional sign printed you will have four blanks.

Much as Fortran 95 introduced the I0 edit descriptor so it allows F0.d. [And for other descriptors, although G0.d was added even later.] F0.2 will provide the minimal field width with those two digits after the decimal point, which is what you want. Note, though, that you will need to explicitly add a blank after the = sign:

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

[I've also used the : edit descriptor to avoid trailing blanks.]

If you want a truly Fortran 90 answer, as you've tagged, then it won't be as nice but it still can be done.

Community
  • 1
  • 1
francescalus
  • 30,576
  • 16
  • 61
  • 96
  • Thanks @francescalus for the explanation. I was not aware about this I0 descriptor. Yes, I'm bit curious to know what could be the true F90 answer. – hbaromega Jul 06 '15 at 12:49
  • It's rather painful and mostly of historical rather than practical value, but: you can write into a temporary character variable and then remove leading blanks with `adjustl`, then print that result. Minimal width output is so much better and widely supported. – francescalus Jul 06 '15 at 13:14
  • thanks again. It might be a related question, do we have minimal field width for the exponent format as well? It seems that `E0.2` does not work. – hbaromega Jul 07 '15 at 13:42
  • No, `E0.2` isn't allowed. Under F2008 the allowed minimal width descriptors are `I`, `B`, `O`, `Z`, `F`, and `G`. `G` being introduced in F2008, the others in F95. – francescalus Jul 07 '15 at 15:31
  • Although `E` is slightly different: if you know the scale factor, the sign and sign mode, and whether the leading optional zero is printed you know how many characters will be non-blank before the `.` This is slightly less effort than for some other types. (If none of that means something to you that's probably a new question.) – francescalus Jul 07 '15 at 18:44
  • It also appears that the current draft of the next version of Fortran would allow `E0.2`. – francescalus Jul 15 '15 at 16:31