C+ C NAME: C Dbl2Str C PURPOSE: C Convert floating point number to string C CATEGORY: C Strings: write floating point number to string C CALLING SEQUENCE: integer function Dbl2Str(Fin,Nin,cFlt) C INPUTS: C Fin double precision floating point number to be converted C Nin integer (see PROCEDURE) C OUTPUTS: C cFlt character*(*) resulting string C Dbl2Str integer number of useful characters in cFlt C INCLUDE: include 'str2str_inc.h' C CALLS: C icompress, Int2Str, Int2StrSet, LocFirst C SEE ALSO: C cFlt2Str, Int2Str, Str2Str C PROCEDURE: C > Round-off is done properly (I think) C > Maximum number of digits behind the decimal point is 8 C > ndig=mod(Nin,100) is the number of digits behind the decimal point C > The output string is made as short as possible. Trailing zero's in the C decimal fraction are dropped (unless ndig < 0). A zero decimal fraction C is dropped, including decimal point. C > If ndig < 0 then trailing zero's in the decimal fraction are retained C (this implies that a zero fraction is not automatically dropped) C > nexp = abs(Nin)/100 determines the range of numbers printed in F C format: 0.1 <= Fin < 10^nexp is printed in F format. Numbers outside C this range are printed in E format. C MODIFICATION HISTORY: C AUG-1995, Paul Hick (UCSD) C- double precision Fin integer Nin character cFlt*(*) parameter (LG = 16) parameter (LD = 8) character cTmp*(LG) character cGFmt*7 /'(G16.8)'/ character cFrmt*8 /'(F00.00)'/ logical bExp logical bTrim logical bNegative double precision F double precision Frac bNegative = Fin .lt. 0 F = abs(Fin) bTrim = Nin .ge. 0 nDig = mod(abs(Nin),100) I = min(abs(Nin)/100,8) if (I .eq. 0) I = LD write (cGFmt(6:6),'(I1)') I write (cTmp,cGFmt) F iE = LG-4+1 ! Position exponent bExp = cTmp(iE:iE) .eq. 'E' ! Exponent present if (bExp) then read (cTmp(iE+1:),'(I3)') iExp ! Read exponent bExp = iExp .ne. 0 ! Throw away zero exponent cTmp(iE:) = ' ' end if M = LocFirst('.',cTmp) N = iE-1 ! N-M = # digits do while (cTmp(:1) .eq. ' ' .and. N-M .lt. nDig) cTmp(:N-1) = cTmp(2:N) cTmp(N:N) = '0' M = M-1 end do write (cFrmt,'(A2,I2.2,A1,I2.2,A1)') '(F',N,'.',N-M,')' read (cTmp(:iE-1),cFrmt) Frac ! Read fraction N = min(iE-1,M+nDig) write (cFrmt,'(A2,I2.2,A1,I2.2,A1)') '(F',N,'.',N-M,')' ! Write fraction in F format cTmp = ' ' I = 1 if (bExp) write (cTmp(:N),cFrmt,iostat=I) 10*Frac if (I .eq. 0) then iExp = iExp-1 bExp = iExp .ne. 0 else write (cTmp(:N),cFrmt,iostat=I) Frac end if if (bTrim) then do while (cTmp(N:N) .eq. '0') N = N-1 end do end if cTmp(N+1:) = ' ' if (cTmp(N:N) .eq. '.') then cTmp(N:N) = ' ' ! Remove dot N = N-1 end if if (cTmp .eq. ' ') then ! Needed for MS-Fortran cTmp = '0' N = 1 end if if (bExp) then N = N+1 cTmp(N:) = 'E' I = Int2StrSet(STR__TRIM) N = N+Int2Str(iExp,cTmp(N+1:)) I = Int2StrSet(I) end if N = icompress(cTmp,cTmp) if (bNegative) then cFlt = '-'//cTmp(:N) N = N+1 else cFlt = cTmp(:N) end if Dbl2Str = N return end