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
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
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?
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?
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?
I think the following approach will work:
CODE
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?
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 -->
RE: How to find first non-zero decimal place?
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?
A simple test to exclude Intergers looks to work. Something like:
CODE
RE: How to find first non-zero decimal place?
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 -->
RE: How to find first non-zero decimal place?