print a la C [message #9059] |
Thu, 29 May 1997 00:00  |
Andrea Spinelli
Messages: 4 Registered: August 1996
|
Junior Member |
|
|
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
|
|
|
Re: print a la C [message #9107 is a reply to message #9059] |
Wed, 04 June 1997 00:00  |
knipp
Messages: 68 Registered: January 1993
|
Member |
|
|
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
|
|
|
Re: print a la C [message #9243 is a reply to message #9059] |
Wed, 04 June 1997 00:00  |
R. Bauer
Messages: 137 Registered: November 1996
|
Senior Member |
|
|
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
|
|
|