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 #46168 is a reply to message #46167] Sat, 05 November 2005 07:31 Go to previous messageGo to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
David Fanning <david@dfanning.com> writes:
> Folks,
>
> I've run into an interesting problem. I have a double precision
> number that I wish to convert to a string (so I can put it into
> a text widget, for example). I don't know ahead of time how many
> significant digits will be in this number. The number could
> look like this 45.6, or this 123456789.123456789, or this
> -22.1234567890. If it looks like the latter number, I am
> having a very hard time converting it to a string. For example,
> this doesn't work:
>
> IDL> v = String(-22.1234567890, Format='(D0)')
> IDL> Print, v
> -22.123457
>
> Is this a bug in IDL? Or am I overlooking something?

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 20:14:36 PDT 2025

Total time taken to generate the page: 1.22148 seconds