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

Re: significant figures function?



"Med Bennett" <mbennett@indra.com> wrote in message
3A9ACD30.E0F3F5D9@indra.com">news:3A9ACD30.E0F3F5D9@indra.com...
> Has anyone written a function that returns the input value or array with
> a specified number of significant digits?  I have lloked at the various
> IDL libraries on the web but did not come up with what I'm after.
> Thanks in advance for any pointers -
>

this should do it

function fix_digits, num, digits
 expon = 1.0
 while num/expon gt 1 do expon = expon * 10
 fix_val = num/expon
 fstring = string(digits+2, digits, format = '("(f",i0.0,".",i0.0,")")')
 reads, string(fix_val, format = fstring), fix_val
 return, fix_val * expon
end

> print, fix_digits(!pi, 3)
      3.14000


Martin

----------------------------------------
Mr. Martin Downing,
Clinical Research Physicist,
Orthopaedic RSA Research Centre,
Woodend Hospital,
Aberdeen, AB15 6LS.
m.downing@abdn.ac.uk


Martin