For Programmers: Free Programming Magazines  


Home > Archive > Fortran > December 2004 > Re: Outputting integers with minimum width









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Outputting integers with minimum width
Michel OLAGNON

2004-12-15, 3:59 pm


In article <41c046e9$0$78013$ed2619ec@ptn-nntp-reader01.plus.net>, Simon Geard <simon@quintic.co.uk> writes:
>I want to output the result of some function evaluations, e.g.
>
>f(2) = [20]
>f(10) = [100]
>
>in java I'd do
>
>System.out.println("f(" + m + " = [" + n + "]");
>
>but I can't find a simple way of doing this in f90 since I don't know
>what the field width is in advance. The only way I can think ofdoing it
>is do dynamically create the format string as in the example below, but
>surely there is an easier way of doing this. Any ideas?
>



write(fmt,'(I20)') n
write(*,'("[",a,"]")') trim(adjustl(fmt))


Arjen Markus

2004-12-15, 3:59 pm

Michel OLAGNON wrote:
>
> In article <41c046e9$0$78013$ed2619ec@ptn-nntp-reader01.plus.net>, Simon Geard <simon@quintic.co.uk> writes:
>
> write(fmt,'(I20)') n
> write(*,'("[",a,"]")') trim(adjustl(fmt))


Another way is the format '(I0)'. For instance:

program test123
write(*,'(1x,10(i0,''=''))') 1, 234455, 3, -12
end program

gives:
1=234455=3=-12=

Regards,

Arjen
Gordon Sande

2004-12-15, 3:59 pm



Arjen Markus wrote:
> Michel OLAGNON wrote:
>
>
>
> Another way is the format '(I0)'. For instance:
>
> program test123
> write(*,'(1x,10(i0,''=''))') 1, 234455, 3, -12
> end program
>
> gives:
> 1=234455=3=-12=


This is the F95 solution. The question becomes did the OP mean
F90 or F95 when the question was posed about f90?

It is such a useful feature that it is hard to think that any
"Fortran 90" compiler/run time being offered would not in fact
be Fortran 95. But there are a few folks who must soldier on with
legacy compilers. (Or perhaps they prefer Microsoft Workstation.
Or perhaps their PHB does. ;-))

> Regards,
>
> Arjen

Simon Geard

2004-12-15, 3:59 pm

Gordon Sande wrote:

>
>
> Arjen Markus wrote:
>

....[color=darkred]
>
>
> This is the F95 solution. The question becomes did the OP mean
> F90 or F95 when the question was posed about f90?
>
> It is such a useful feature that it is hard to think that any
> "Fortran 90" compiler/run time being offered would not in fact
> be Fortran 95. But there are a few folks who must soldier on with
> legacy compilers. (Or perhaps they prefer Microsoft Workstation.
> Or perhaps their PHB does. ;-))
>

All the compilers I'm using support f95 so thanks very much for this
solution - very helpful.

Simon
Arjen Markus

2004-12-16, 3:57 am

Gordon Sande wrote:
>
> Arjen Markus wrote:


>
> This is the F95 solution. The question becomes did the OP mean
> F90 or F95 when the question was posed about f90?
>
> It is such a useful feature that it is hard to think that any
> "Fortran 90" compiler/run time being offered would not in fact
> be Fortran 95. But there are a few folks who must soldier on with
> legacy compilers. (Or perhaps they prefer Microsoft Workstation.
> Or perhaps their PHB does. ;-))
>


Oops, I have gotten so used to F95, that this subtle difference
to F90 escaped my attention.

Yes, it is very useful!

Regards,

Arjen
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com