# Re: printing floats/integer

```Sean Heukels <sean77=cuthere=@dds.nl> wrote in message
9881p0\$55o\$1@newshost.accu.uu.nl">news:9881p0\$55o\$1@newshost.accu.uu.nl...
> I wrote a small module for integers. The variable is formatted and
returned.
>
> "1         " returns as "1"
>
> Now this doesn't work with floats. for example if I want to print
"1.22221"
> I
> dont want to see it as "1.222221000000000000000" or
> "1.222222                  "
>
> Does anyone know how I can solve this ??
>
> Greets Sean

Please see the following code.
Note that there is a main level example code at the bottom of the function.

Using a = 123121.22222200000d
I get the following output:
Normal print command:
123121.22
Print command with GetFormat:
123121.222222

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'
formatstring = ''
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)
print,'slen:',slen
print,s
dpos=strpos(s,'.') ; find decimal posiiton
if dpos eq -1 then begin
; this shouldn't happen
print,'Error: no decimal place found'
formatstring = ''
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

; ******    main level code here
a = 123121.22222200000d
;must become 1.22222 without leading or trailing spaces.
b=  1.00

print,'Normal print command:'
print,a
print,'Print command with GetFormat:'
print,a,format=getformatstring(a)

end

```

• Follow-Ups: