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

Home » Public Forums » archive » Re: How to eliminate trailing zeros in a string?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: How to eliminate trailing zeros in a string? [message #33798] Sat, 25 January 2003 10:35
Craig Markwardt is currently offline  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 Go to previous message
marc schellens[1] is currently offline  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 #33802 is a reply to message #33800] Fri, 24 January 2003 15:14 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
James Kuyper (kuyper@saicmodis.com) writes:

> 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.

Well, then, he can use the inelegant and slower solution. :-)

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 #33803 is a reply to message #33802] Fri, 24 January 2003 15:05 Go to previous message
James Kuyper is currently offline  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 #33804 is a reply to message #33803] Fri, 24 January 2003 14:58 Go to previous message
K. Bowman is currently offline  K. Bowman
Messages: 330
Registered: May 2000
Senior Member
In article <FciY9.100408$eB1.3827522@twister.southeast.rr.com>,
K Banerjee <kbanerjee@ucwphilly.rr.com> wrote:

> Say I have the string "20.304000". How can I eliminate the last 3
> zeros?

See STRMID (and possibly STRPOS, etc.).

Ken
Re: How to eliminate trailing zeros in a string? [message #33805 is a reply to message #33804] Fri, 24 January 2003 14:56 Go to previous message
David Fanning is currently offline  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 Go to previous message
David Fanning is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: IDLgrWindow, IDLgrVolume and alpha channel
Next Topic: MOUSE (WHEEL) BUTTON NON-STOP SCROLLING IN IDL 5.6

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

Current Time: Wed Oct 08 15:34:23 PDT 2025

Total time taken to generate the page: 0.00476 seconds