default formatting of integers

I have a question about the formatting of integers in
write statements.

When I have a line of code like

int ndiff

write(,) "number of differences = ",ndiff
or
write(*,’(a,i)’) "number of differences = ",ndiff

I find that the integer variable ndiff is formatted to something
like i12 or some really long width which doesn’t make the output
look too nice.

In this application, the value of “ndiff” can range over about 8 orders
of magnitude. Is there an easy way to have “ndiff” formatted so
its output width approximately matches its value, without having to
put in cascading if-else statements to check the order of magnitude
and then explicitly set the width based on the actual value of ndiff?

Thanks for any suggestions,

Catherine

You could try a format statment

Instead of:

write(*,’(a,i)’) "number of differences = ",ndiff

Try:

write(* ,1) "number of differences = ",ndiff
1 format(a,d14.12)

For smaller integers, you’ll have a lot of zeros at the end, but all of the numbers will be lined up. Like, if you wanted to make a table.

What I would really like is to have a small width for small
integers, and a large width for larger numbers without having
to explicitly test for the minimum width needed to contain
the integer.

The SGI compiler automatically does this (the compiler that I’m
switching from), and I was wondering if the PG compiler has any
such capability.

Catherine

Hi Catherine,

We currently don’t have an option to print an integer with its width size. A workaround for now would be:

write(myarray,’(i)’) ndiff
write(*,’(a)’) trim(adjustl(myarray))

Hongyon