Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28486 Discussions

How to print real numbers left-padded with zero?

intelgg
Beginner
604 Views
The format i4.4 would print the integer number 12 as "0012". What format would print the real number 12.3 as "0012.3"?
0 Kudos
5 Replies
Steven_L_Intel1
Employee
604 Views
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
0 Kudos
intelgg
Beginner
604 Views
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?
0 Kudos
kdkeefer
Beginner
604 Views
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
0 Kudos
intelgg
Beginner
604 Views
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?)
0 Kudos
Steven_L_Intel1
Employee
604 Views
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
0 Kudos
Reply