How to print real numbers left-padded with zero?

How to print real numbers left-padded with zero?

The format i4.4 would print the integer number 12 as "0012". What format would print the real number 12.3 as "0012.3"?

6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

There is no Fortran format that will do this - sorry. If you need this, you'll have to write into a character variable using internal write and then "post-process" the string replacing leading blanks with zeroes.

Steve

Retired 12/31/2016

Thanks Steve

Can users define new formats?

I can print the real number x = 12.3 as "0012.3" by doing
write(*, '(i4.4,f2.1)') floor(x), x - floor(x).

Can I define a format MY_FORMAT so as to
achieve the same result with
write(*, MY_FORMAT) x?

You can set a character variable to your format code and reference the character string as your format statement.
(It's in the LRM).
Keith

Thanks kdkeefer,

I am afraid your message does address
the issue I am raising.

Note that the format '(i4.4,f2.1)' takes two
inputs, which in my usage happen to be related.

The format MY_FORMAT I am seeking would achieve
the same result with just one input.

(By the way, what is LRM; what does this acronym mean?)

LRM = Language Reference Manual.

No, you cannot invent your own format codes. You could write a function that returns a character result with the value formatted as you like (then display it with the A format.)

Steve

Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today