×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to find first non-zero decimal place?

How to find first non-zero decimal place?

How to find first non-zero decimal place?

(OP)
Hello everyone,

I would like to display numbers with a defined amount of decimal places.

Let's take 0.001

CODE -->

local numbervar Decimals :=2
toText(Value, Decimals)
This would result in "0.00". 

So, if the resulting number where to only contain zeros, I would like to increase the amount of decimals to the first non-zero.

Does anybody know a formula that counts the amount of decimal zeros?

Only the ones before the first non-zero of course, i.e. 0.0010002 should return 2 and not 5.
Alternatively, the formula can return the position of the first non-zero, i.e. 3, instead of the amount of zeros, i.e. 2. :)

Thank you very much :)

Alex

RE: How to find first non-zero decimal place?

It is not really clear what you are trying to achieve here. Can you provide a few examples of the actual data and what you want displayed.

An explanation of why you are doing this would also be useful as I am struggling to see a business case for this.

Regards
Pete.

RE: How to find first non-zero decimal place?

(OP)
Sure thing.

So, let's say I have a report displaying a number 0.0001234. Now the size of that report field is limited (say 1 cm width). A decimal number with 4 leading zeros might not fit in the field.

So I can define a macimum amount of decimals by using toText(Number, Decimals).

But, if I define e.g. 2 decimals a number with value 0.001234 will be shown as 0.00.

So I need to find out where the first non-zero decimal is and then use this variable as the number of decimals to display.

0.00001234 would need to be toText(0.00001234,5).
So I need to calculate the position of "1", i.e. the 5th position.

RE: How to find first non-zero decimal place?

Hi Alex

I think the following approach will work:

CODE

IF      ({Table.Field} - TRUNCATE({Table.Field})) * 1       >= 1
THEN    0 
ELSE
IF      ({Table.Field} - TRUNCATE({Table.Field})) * 10      >= 1
THEN    1 
ELSE
IF      ({Table.Field} - TRUNCATE({Table.Field})) * 100     >= 1
THEN    2 
ELSE
IF      ({Table.Field} - TRUNCATE({Table.Field})) * 1000    >= 1
THEN    3
ELSE
IF      ({Table.Field} - TRUNCATE({Table.Field})) * 10000   >= 1
THEN    4  
ELSE
IF      ({Table.Field} - TRUNCATE({Table.Field})) * 100000  >= 1
THEN    5  
ELSE
IF      ({Table.Field} - TRUNCATE({Table.Field})) * 1000000 >= 1
THEN    6 

Depending on the number of decimals you are dealing with, you may need to add extra lines to the formula.

Hope this helps.

Cheers
Pete.

RE: How to find first non-zero decimal place?

(OP)
Perfect!

I modified it to be independent of the amount of decimals. Although maybe I will limit to 99 to prevent the system from crashing. But it seems CR will simply output an error rather than crash. Or simply add an `If Number<>0`

CODE -->

local numbervar Number:=10.00000123;
local numbervar Decimals:=0;
local numbervar End_Bit:=0;

While End_Bit=0 Do
    (
    If (Number - TRUNCATE(Number)) * (10^Decimals) >= 1 Then
        End_Bit:=1
    Else
        Decimals:=Decimals+1
    );
Decimals 

RE: How to find first non-zero decimal place?

(OP)
@pmax9999
I just found that if the value does not have any decimals, i.e. an integer, it seems the code no longer works.

So if `Number:=10` the code will not work.

RE: How to find first non-zero decimal place?

Not sure why your implementation using the loop doesn't work. Something to do with the 0 Exponent it doesn't like.

A simple test to exclude Intergers looks to work. Something like:

CODE

local numbervar Number:=10.0;
local numbervar Decimals:=0;
local numbervar End_Bit:=0;

If      TRUNCATE(Number) <> Number
Then    (
            While End_Bit=0 Do
                (
                If (Number - TRUNCATE(Number)) * (10^Decimals) >= 1 Then
                    End_Bit:=1
                Else
                    Decimals:=Decimals+1
                )
        );
Decimals 

RE: How to find first non-zero decimal place?

(OP)
Yes, I opted for the exact same solution. smile

In addition I added an iteration limit of 99 (to prevent error messages whenever the number is 0)

I think the error is, that the first case, i.e. 0 leading zeros means that there is a decimal.
But if there is no decimal at all, then
number-trunc(number)=0.
Multiplied by 1 is still 0.

So there are three outcomes.
number - trunc(number
= 0 -> no decimals
= 0 < x < 1 -> leading zero
>= 1 -> first non zero decimal

CODE -->

If ((Number - TRUNCATE(Number)) * (10^Decimals) >= 1) OR (Number- Truncate(Number)=0) Then
                    End_Bit:=1
                Else
                    Decimals:=Decimals+1
                )
        );
Decimals 


RE: How to find first non-zero decimal place?

(OP)
Do you, @pmax9999, or someone else know if it is possible to check if the number following the first non-zero decimal is itself a zero?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close