Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
Hi, does anybody know whether there is a freely available somewhere a function for printing floats in a way similar to the %g argument of printf? E.g.: printf("%g\n",0.02); [C] results in 0.02 print, 0.01 [IDL] 0.0100000 (no extra blanks, no extra zeroes) Of course, I could say in IDL print,format='(f4.2)',0.01 but I need something that, without knowing the number of digits in advance, makes an earnest effort to save space and keep readability (and works, e.g., also with 0.003!!!) Thanks in advance, Andrea Spinelli spinellia@acm.org
In article <338D9954.344A@acm.org>, Andrea Spinelli <spinellia@acm.org> writes: > Hi, > > does anybody know whether there is a freely available somewhere a > function for printing floats in a way similar to the %g > argument of printf? E.g.: > > printf("%g\n",0.02); [C] > results in > 0.02 > > print, 0.01 [IDL] > 0.0100000 > > (no extra blanks, no extra zeroes) > > Of course, I could say in IDL > > print,format='(f4.2)',0.01 > > but I need something that, without knowing the number of > digits in advance, makes an earnest effort to save space > and keep readability (and works, e.g., also with 0.003!!!) > > Thanks in advance, > Andrea Spinelli > spinellia@acm.org Andrea, do something like: - convert number to string via s_num = strtrim(string(number), 2) - get last non-zero char. via strpos - print Karl --- knipp@digitalmap.hi.bosch.de
Andrea Spinelli wrote: > > Hi, > > does anybody know whether there is a freely available somewhere a > function for printing floats in a way similar to the %g > argument of printf? E.g.: > > printf("%g\n",0.02); [C] > results in > 0.02 > > print, 0.01 [IDL] > 0.0100000 Hi Andrea, I can share one of my functions with you. num_dec is written by Ray Sterner and you can get it from his library. http://fermi.jhuapl.edu/res/ ;Copyright R.Bauer ;Institut fuer stratosphaerische Chemie function number_format,number,format=format,help=help IF keyword_set(help) THEN BEGIN print,'(number_forms) no_string=number_forms(number)' print,'use format=format to get the Format code' return,-1 ENDIF ab = strtrim(string(number),2) dp = strtrim(string(num_dec(number)),2) length = strtrim(string(strpos(ab,'.')+1+long(dp)),2) form = "(f"+length+"."+dp+")" IF keyword_set(format) THEN BEGIN return,form ENDIF return,string(format=form,number) END -- R.Bauer Institut fuer Stratosphaerische Chemie (ICG-1) Forschungszentrum Juelich email: R.Bauer@fz-juelich.de