[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: printing floats/integer (ACK, new function deined here GetFormatString.pro)



ACK, I sent off the first version of this code before finishing
a small piece (i.e. how I returned out of error situations).
Here is the "final code".

Cheers,
bob stockwell
stockwell@co-ra.com




function getformatstring,number

; this should be big enough to show everything
showallformatcommmand = '(f50.25)'

nsize = size(number,/st)
case nsize.type of
;0:  Undefined
;1:  Byte
;2:  Integer
;3:  Longword integer
4: begin;  Floating point
 s =  STRING(FORMAT=showallformatcommmand, number)
 s = strtrim(s,1) ; remove leading blanks
 slen = strlen(s)
 dpos=strpos(s,'.') ; find decimal posiiton
 if dpos eq -1 then begin
  ; this shouldn't happen
  print,'Error: no decimal place found'
  return,'' ; null string
 endif else begin
  ; chop off lagging 0s
  zpos=0
  doflag=1
  while doflag do begin ; find how many lagging zeros there are
   char = strmid(s,zpos,1,/reverse)
   if char eq '0' then begin
    zpos=zpos+1
   endif else begin
    doflag=0
   endelse
  endwhile
 endelse
 end
5:  begin;  Double-precision floating
 s =  STRING(FORMAT=showallformatcommmand, number)
 s = strtrim(s,1) ; remove leading blanks
 slen = strlen(s)
 dpos=strpos(s,'.') ; find decimal posiiton
 if dpos eq -1 then begin
  ; this shouldn't happen
  print,'Error: no decimal place found'
  return,'' ; null string
 endif else begin
  ; chop off lagging 0s
  zpos=0
  doflag=1
  while doflag do begin ; find how many lagging zeros there are
   char = strmid(s,zpos,1,/reverse)
   if char eq '0' then begin
    zpos=zpos+1
   endif else begin
    doflag=0
   endelse
  endwhile
 endelse
 end
;6:; Complex floating
;7:  String
;8:  Structure
;9:; Double-precision complex
;10: Pointer
;11: Object reference
;12:; Unsigned Integer
;13:; Unsigned Longword Integer
;14:; 64-bit Integer
;15:; Unsigned 64-bit Integer
else: begin
 print,'Error: Number not a supported type.'
 return,'' ; null string
endelse
endcase

d =slen-dpos-zpos-1
w = dpos+d+1 ; 1 for the decimal place
formatstring = strcompress("(f"+string(w)+'.'+string(d)+")",/rem)
return,formatstring

end ; end of function





; __________________________________________
; ******    main level code here ***********
a = 123121.22222200000d
;must become 1.22222 without leading or trailing spaces.
print,'Normal print command:'
print,a
print,'Print command with GetFormat:'
print,a,format=getformatstring(a)

end