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 
Return to the default flat view Create a new topic Submit Reply
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
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: Fri Oct 10 14:28:08 PDT 2025

Total time taken to generate the page: 0.31792 seconds