How can I left-justify numerical output in fortran?
Asked Answered
P

5

12

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!

Presnell answered 28/12, 2010 at 21:49 Comment(0)
F
8

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, ...

Felly answered 28/12, 2010 at 23:49 Comment(1)
I had hoped for something more elegant, but was seeming to settle on a similar solution. Thanks for the answer @ubuntouristPresnell
T
8

This is easier with Fortran 95, but still not trivial. Write the number or other item to a string with a write statement (as in the first answer). Then use the Fortran 95 intrinsic "ADJUSTL" to left adjust the non-blank characters of the string.

Tether answered 29/12, 2010 at 5:29 Comment(1)
There's a great tutorial of this here: jblevins.org/log/leftjustQuant
E
2

And really un-elegant is my method (I program like a cave woman), after writing the simple Fortran write format (which is not LJ), I use a combination of Excel (csv) and ultraedit to remove the spaces effectively getting the desired LJ followed directly by commas (which I need for my specific import format to another software). BF

Erlene answered 12/3, 2014 at 16:5 Comment(0)
I
1

If what you really want is whitespace between output fields rather than left-justified numbers to leave whitespace you could simply use the X edit descriptor. For example

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

will insert 4 spaces between each field and the next. Note that the standard requires 1X for one space, some of the current compilers accept the non-standard X too.

Inquisitor answered 12/11, 2013 at 11:21 Comment(0)
A
0

!for left-justified float with 1 decimal.. the number to the right of the decimal is how many decimals are required. Write rounds to the desired decimal space automatically, rather than truncating.

write(*, ['(f0.1)']) RValue !or
write(*, '(f0.1)') RValue 

!for left-justified integers..

write(*, ['(i0)']) intValue !or
write(*, '(i0)') RValue 

*after feedback from Vladimir, retesting proved the command works with or without the array brackets

Amsden answered 28/11, 2022 at 2:24 Comment(11)
Why do you use an array for the format string?Bootlace
string is not a native data type, in many flavors of fortran.. they are character arrays, written similarly to a sql definition character*20 or character(20). When you leave them open in length, it requires a pointer value.Amsden
Your last comments does not make any sense. 1. character strings absolutely are a Fortran native datatype, 2. character arrays are completely different, 3. ['text'] is an array of character strings with one element, not an array of characters.Bootlace
keyword [string] is not supported. character(len=:),allocatable :: line ! is equivalent to a stringAmsden
That does not matter. The keyword character is used for strings. The syntax is character(len) where len is the length. It is not "equivalent" to a string, it IS a string. It is NOT a character array.Bootlace
However, ['(i0)'] is an array of strings. An array of length one. It is not an error, it shall work. But it is strange. Just '(i0)' is enough. That is one string.Bootlace
character(8) is equivalent to NTEXT, bc the length is always allocated to the same sizeAmsden
NTEXT is not a Fortran term. The Fortran standard knows character string and character of any length, deferred or fixed, does not matter, is a character string.Bootlace
Let us continue this discussion in chat.Amsden
I think there is still some misunderstanding. The square brackets are not required anywhere. Nowhere at all. Really. They are only, in general, used in documentation and manuals of various programming languages. See, for example, gcc.gnu.org/onlinedocs/gfortran/… Here all three arguments are in the square brackets and are optional ([SIZE, PUT, GET]). But you certainly cannot put the brackets there. It is just a coincidence they they do work in your case.Bootlace
In your example a reader would assume either one of the 3 terms is may be used, or that all three are optional. in the latter case (,,GET) would be an option. to be clear.Amsden

© 2022 - 2024 — McMap. All rights reserved.