Re: variable length of integer
- From: John Appleyard <spamtrap@xxxxxxxxxxxxxx>
- Date: Tue, 29 Apr 2008 14:48:11 +0100
relaxmike wrote:
What we really need is the following :
type ( varying_string ) :: mystring
mystring = stringformat ( 2008 )
call stringprint ( mystring )
but we do not have this in standard fortran.
Any other idea ?
Regards,
Michaël Baudin
You can use the techniques you describe to write a "stringformat" or "IntToString" function in Fortran. That can then be used in concatenations, I/O statements or whatever, so that you can get away without the varying_string type.
Here's one I use - it has 2 different ways to compute the string length. RealToString is altogether more complicated.
program testIntToString
print *,('"',inttostring(i),'",',i=-100,100)
contains
function IntToString(i) ! This version can be used in I/O statements
implicit none
integer :: i
! The following elaborate declaration is required to define a string
! having just enough positions to hold all the digits of the integer,
! and the sign character.
! It contains constants no greater than 10000, so as to be acceptable
! on machines having a default integer kind of 16 bits or more.
! (borrowed from "Algorithms and Data Structures in F and Fortran"
! By Robin A. Vowels - Published by Unicomp, 1998. ISBN 0-9640135-4-1)
! character (len = 1 + & ! For 1-digit
! min (abs(i/10), 1) + & ! For 2-digit->1
! min (abs(i/100), 1) + & ! For 3-digit->1
! min (abs(i/1000), 1) + & ! For 4-digit->1
! min (abs(i/10000), 1) + & ! For 5-digit->1
! min (abs(i/10000)/10, 1) + & ! For 6-digit->1
! min (abs(i/10000)/100, 1) + & ! For 7-digit->1
! min (abs(i/10000)/1000, 1) + & ! For 8-digit->1
! min (abs(i/10000)/10000, 1) + & ! For 9-digit->1
! min (abs(i/10000)/10000/10, 1) + & ! For 10 digits
! min (abs(i/10000)/10000/100, 1) + & ! For 11 digits
! min (abs(i/10000)/10000/1000, 1) + & ! For 12 digits
! min (abs(i/10000)/10000/10000, 1) + & ! For 13 digits
! min (abs(i/10000)/10000/10000/10, 1) + & ! For 14 digits
! min (abs(i/10000)/10000/10000/100, 1) + & ! For 15 digits
! min (abs(i/10000)/10000/10000/1000, 1) + & ! For 16 digits
! min (abs(i/10000)/10000/10000/10000, 1) + & ! For 17 digits
! min (abs(i/10000)/10000/10000/10000/10, 1) + & ! For 18 digits
! min (abs(i/10000)/10000/10000/10000/100,1) - & ! For 19 digits
! min (max(i, -1), 0) & ! For -ive nums
! ) :: IntToString
character( max(1, int(log10(real(abs(i))+0.1)) + (3-sign(1,i))/2) )&
:: IntToString
integer :: val , p , izero=iachar('0')
if ( i<0 ) then
IntToString(1:1) = '-'
elseif ( i==0 ) then
IntToString(1:1) = '0'
endif
val = abs(i)
p = len(IntToString)
do while( val>0 )
IntToString(p:p) = achar(izero + mod(val,10))
val = val/10
p = p - 1
end do
end function IntToString
end program testIntToString
--
John Appleyard - (send email to john!news@.. rather than spamtrap@..)
Polyhedron Software
Programs for Programmers - QA, Compilers, Graphics, Consultancy
********* Visit our Web site on http://www.polyhedron.co.uk/ *********
.
- Follow-Ups:
- Re: variable length of integer
- From: jamesgiles@xxxxxxx
- Re: variable length of integer
- References:
- variable length of integer
- From: rudra
- Re: variable length of integer
- From: Clive Page
- Re: variable length of integer
- From: Richard Maine
- variable length of integer
- Prev by Date: Re: Allocatable arrays in derived types
- Next by Date: Re: Using external subroutines in OpenMP?
- Previous by thread: Re: variable length of integer
- Next by thread: Re: variable length of integer
- Index(es):
Relevant Pages
|
|