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

Home » Public Forums » archive » Re: Format code to trim leading zeroes?
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: Format code to trim leading zeroes? [message #81225] Tue, 28 August 2012 16:33
Phillip Bitzer is currently offline  Phillip Bitzer
Messages: 223
Registered: June 2006
Senior Member
How about a little STREGEX voodoo?

a = 0.00350800
str1 = stregex(a, '[1-9].*$', /extract)
print, str1
--->350800
Re: Format code to trim leading zeroes? [message #81238 is a reply to message #81225] Tue, 28 August 2012 08:05 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
On Tuesday, August 28, 2012 1:10:09 AM UTC-4, A J wrote:
> I am not sure this can be done easily, but I was looking for some way
>
> to format numbers in order to return the number without leading zeroes
>
> or the decimal. For example,
>
>
>
> if I have
>
>
>
> 0.003508
>
>
>
> I want to return
>
>
>
> 3508
>
>
>
> I have been playing with the format codes and strsplit but nothing has
>
> worked quite right. I am thinking I could write a little program to
>
> feed the number into a string array and run a few steps on the array
>
> such that once the array value no longer equals '0' or '.', it stops
>
> and returns whatever is left. However, before I go down this road, I
>
> thought it might be wise to ask if this is the best/only way to do it.

We kind of have to assume you know how many digits you want. For example, we have to assume you made the conscious decision that 3508 is the number you want, and not 35080 or 3508000, or for that matter 35079999999999. Because you se, in terms of floating point arithmetic 0.003508 0.0035080 0.003508000 and 0.0035079999999999 are all numerically equivalent.

So if you know that you always want a number to the millionth, then why not just compute this?
Y = ROUND(0.003508 / 0.000001)
Now it's an integer and you can use the '(I0)' format string.

If always want a certain number of significant digits, well then that gets a little harder. I would use this,
DENOM = floor(alog10(abs(0.003508))) - 4 ;; 4 digits
Y = ROUND(0.003508 / 10^DENOM)
The alog10() gives you the logarithm, which is the leading significant digit.

Craig
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: image interplate and overlay lat/lon?
Next Topic: Re: "Expression Must be a Scalar in this context:" error in nlinlsq function

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

Current Time: Wed Oct 08 15:06:49 PDT 2025

Total time taken to generate the page: 0.00531 seconds