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

Re: significant figures function?



Thanks Ben and Martin!  I had already kluged together another solution using a
routine I found using the search engine I found using
http://www.astro.washington.edu/deutsch/idl/htmlhelp/index.html (very handy
tool!) by searching for 'significant' called strnsignif.pro
(http://www.astro.washington.edu/deutsch-bin/getpro/library03.html?STRNSIGNIF).
Wrapping this with a loop and converting back to float did the trick.  Less
than perfect, but in the consulting biz, whatever can be found quickly beats
an elegant solution!

Med

Martin Downing wrote:

> "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