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 #33805 is a reply to message #33804] Fri, 24 January 2003 14:56 Go to previous messageGo 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
[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:29:40 PDT 2025

Total time taken to generate the page: 0.40325 seconds