comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Converting Doubles to Strings
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Converting Doubles to Strings [message #46167 is a reply to message #46166] Sat, 05 November 2005 17:01 Go to previous messageGo to previous message
biophys is currently offline  biophys
Messages: 68
Registered: July 2004
Member
Hi, Craig

I tried your inputform_float and It seems that it won't do max
precision with '(d)',

IDL> print,inputform_float(-22.1234567890, '(d0)', /dconvert)
-22.123457D

IDL> print,inputform_float(-22.1234567890, '(d)', /dconvert)
-22.1234570D



Craig Markwardt wrote:

> David, the "D" format (without zeroes) is probably what you want. I
> have a utility routine in INPUTFORM which converts a floating point
> number to a string. It tries both the G and E formats and takes the
> shortest version that is still correct. See below.
>
> Craig
>
> ;; Convert a floating style value to a string. Note the conversion
> ;; happens twice, once as a E and once as a G. The shortest correct
> ;; version of the two is used.
> ;; X - number to convert, scalar or array, float or double
> ;; FORMAT - optional format to use (set to '(E)' or '(D)' for max precision)
> ;; DCONVERT - set this if the output should be double precision
> function inputform_float, x, format, dconvert=dcon
> n = n_elements(x)
> str = string(x(*), format=format)
> sz = size(x) & tp = sz(sz(0)+1)
>
> ;; Sorry, there appears to be no other way to make nice looking
> ;; floating point numbers.
> str1 = string(x(*), format='(G0)')
> if tp EQ 4 then x1 = float(str1)
> if tp EQ 5 then x1 = double(str1)
> wh = where(x-x1 EQ 0, ct)
> if ct GT 0 then str(wh) = str1(wh)
> str1 = 0
> str = strtrim(str,2)
>
> p = strpos(str(0), 'E') ;; Make sure at least one element is float-type
> ;; Note, the space is needed in case the string is placed inside
> ;; another expression down the line.
> if p LT 0 then begin
> if keyword_set(dcon) then str(0) = str(0) + 'D' $
> else str(0) = str(0) + 'E'
> endif
> if keyword_set(dcon) then begin
> ;; Convert from floating to double
> p = strpos(str, 'E')
> wh = where(p GE 0, ct)
> for i = 0L, ct-1 do begin
> str1 = str(wh(i))
> strput, str1, 'D', p(wh(i))
> str(wh(i)) = str1
> endfor
> endif
> ;; Construct format like (N(A,:,","))
> fmt = '('+strtrim(n,2)+'(A,:,","))'
> return, string(str, format=fmt)
> end
>
>
>
> --
> ------------------------------------------------------------ --------------
> Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
> Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
> ------------------------------------------------------------ --------------
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Displaying three images simultaneously (using Object Graphics)
Next Topic: Specifying Log Axis Tick Marks

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 18:11:33 PDT 2025

Total time taken to generate the page: 0.00228 seconds