|
Re: Format code to trim leading zeroes? [message #81238 is a reply to message #81225] |
Tue, 28 August 2012 08:05  |
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
|
|
|