Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find first non-zero decimal place?

Status
Not open for further replies.

AleXSR700

Technical User
Jul 20, 2023
17
DE
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
 
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.
 
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.
 
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.
 
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
 
@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.
 
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

 
Yes, I opted for the exact same solution. :)

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


 
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?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top