How can I left-justify numerical output in fortran?

mishaF picture mishaF · Dec 28, 2010 · Viewed 11.1k times · Source

I am writing some simple output in fortran, but I want whitespace delimiters. If use the following statement, however:

format(A20,ES18.8,A12,ES18.8)

I get output like this:

p001t0000               3.49141273E+01obsgp_oden      1.00000000E+00

I would prefer this:

p001t0000           3.49141273E+01   obsgp_oden  1.00000000E+00

I tried using negative values for width (like in Python) but no dice. So, is there a way to left-justify the numbers?

Many thanks in advance!

Answer

Ubuntourist picture Ubuntourist · Dec 29, 2010

There's not a particularly beautiful way. However, using an internal WRITE statement to convert the number to a text string (formerly done with an ENCODE statement), and then manipulating the text may do what you need.

Quoting http://rsusu1.rnd.runnet.ru/develop/fortran/prof77/node168.html

An internal file WRITE is typically used to convert a numerical value to a character string by using a suitable format specification, for example:

  CHARACTER*8 CVAL 
  RVALUE = 98.6 
  WRITE(CVAL, '(SP, F7.2)') RVALUE

The WRITE statement will fill the character variable CVAL with the characters ' +98.60 ' (note that there is one blank at each end of the number, the first because the number is right-justified in the field of 7 characters, the second because the record is padded out to the declared length of 8 characters).

Once a number has been turned into a character-string it can be processed further in the various ways described in section 7. This makes it possible, for example, to write numbers left-justified in a field, ...