Re: How to eliminate trailing zeros in a string? [message #33798] |
Sat, 25 January 2003 10:35 |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
David Fanning <david@dfanning.com> writes:
>
> Alright, I've thought about it a little bit. How about this:
>
> IDL> str = '20.304000'
> IDL> str = String(str, Format='(F6.3)')
> IDL> Print, str
> 20.304
How about this variation on David's technique, but satisfies James's
requirement of an arbitrary number of zeroes:
print, string(str, format='(G0)')
The "G0" format usually renders a number in its most compact form.
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|
Re: How to eliminate trailing zeros in a string? [message #33800 is a reply to message #33798] |
Sat, 25 January 2003 03:59  |
marc schellens[1]
Messages: 183 Registered: January 2000
|
Senior Member |
|
|
K Banerjee wrote:
> Folks,
>
> Say I have the string "20.304000". How can I eliminate the last 3
> zeros?
>
> Thanks.
>
> K. Banerjee
>
How about this one:
s=inputString
b=byte(s)
i=max(where(b ne 48))
trim=string(b[0:i])
This won't work with multiple of 10 integers (when no decimal point
is there).
BTW: Is there a specification, that the where function gives its result
always in ascending order?
cheers,
marc
|
|
|
|
Re: How to eliminate trailing zeros in a string? [message #33803 is a reply to message #33802] |
Fri, 24 January 2003 15:05  |
James Kuyper
Messages: 425 Registered: March 2000
|
Senior Member |
|
|
David Fanning wrote:
>
> David Fanning (david@dfanning.com) writes:
>
>> K Banerjee (kbanerjee@ucwphilly.rr.com) writes:
>>
>>> Say I have the string "20.304000". How can I eliminate the last 3
>>> zeros?
>>
>> Oh, man, this is going to get me banished from the IDL
>> Expert Programmers Association, but what about this
>> little function:
>>
>> ;***********************************************************
>> FUNCTION StripZeros, str
>>
>> IF N_Elements(str) EQ 0 THEN $
>> theString = '20.304000' ELSE $
>> theString = str
>>
>> char = (Reverse(Byte(theString)))[0]
>> IF char NE 48 THEN RETURN, theString ELSE BEGIN
>> WHILE char EQ 48 DO BEGIN
>> theString = StrMid(theString, 0, StrLen(theString)-1)
>> char = (Reverse(Byte(theString)))[0]
>> ENDWHILE
>> ENDELSE
>> RETURN, theString
>> END
>> ;***********************************************************
>>
>> Run it like this:
>>
>> IDL> Print, StripZeros('20.304000')
>> 20.304
>>
>> I'm not claiming elegant here, but it took me 3 minutes, tops!
>> I'm *sure* I could make it shorter if I had more time. :-)
>
> Alright, I've thought about it a little bit. How about this:
>
> IDL> str = '20.304000'
> IDL> str = String(str, Format='(F6.3)')
> IDL> Print, str
> 20.304
I'd assume from the title that he wants to remove ALL of the trailing
zeros, no matter how many or how few they are.
|
|
|
|
Re: How to eliminate trailing zeros in a string? [message #33805 is a reply to message #33804] |
Fri, 24 January 2003 14:56  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning (david@dfanning.com) writes:
> K Banerjee (kbanerjee@ucwphilly.rr.com) writes:
>
>> Say I have the string "20.304000". How can I eliminate the last 3
>> zeros?
>
> Oh, man, this is going to get me banished from the IDL
> Expert Programmers Association, but what about this
> little function:
>
> ;***********************************************************
> FUNCTION StripZeros, str
>
> IF N_Elements(str) EQ 0 THEN $
> theString = '20.304000' ELSE $
> theString = str
>
> char = (Reverse(Byte(theString)))[0]
> IF char NE 48 THEN RETURN, theString ELSE BEGIN
> WHILE char EQ 48 DO BEGIN
> theString = StrMid(theString, 0, StrLen(theString)-1)
> char = (Reverse(Byte(theString)))[0]
> ENDWHILE
> ENDELSE
> RETURN, theString
> END
> ;***********************************************************
>
> Run it like this:
>
> IDL> Print, StripZeros('20.304000')
> 20.304
>
> I'm not claiming elegant here, but it took me 3 minutes, tops!
> I'm *sure* I could make it shorter if I had more time. :-)
Alright, I've thought about it a little bit. How about this:
IDL> str = '20.304000'
IDL> str = String(str, Format='(F6.3)')
IDL> Print, str
20.304
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|
Re: How to eliminate trailing zeros in a string? [message #33806 is a reply to message #33805] |
Fri, 24 January 2003 14:42  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
K Banerjee (kbanerjee@ucwphilly.rr.com) writes:
> Say I have the string "20.304000". How can I eliminate the last 3
> zeros?
Oh, man, this is going to get me banished from the IDL
Expert Programmers Association, but what about this
little function:
;***********************************************************
FUNCTION StripZeros, str
IF N_Elements(str) EQ 0 THEN $
theString = '20.304000' ELSE $
theString = str
char = (Reverse(Byte(theString)))[0]
IF char NE 48 THEN RETURN, theString ELSE BEGIN
WHILE char EQ 48 DO BEGIN
theString = StrMid(theString, 0, StrLen(theString)-1)
char = (Reverse(Byte(theString)))[0]
ENDWHILE
ENDELSE
RETURN, theString
END
;***********************************************************
Run it like this:
IDL> Print, StripZeros('20.304000')
20.304
I'm not claiming elegant here, but it took me 3 minutes, tops!
I'm *sure* I could make it shorter if I had more time. :-)
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting, Inc.
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|