How to obtain the amount of digits filled in a NUMERIC field?

Hello! How to obtain the amount of digits filled in a NUMERIC field? My necessity is to check if a numeric variable defined with size 17 is fully filled (contain 17 numbers, not less than this).

I’ve already tried 2 things:

  1. Using MASK, but it doesn’t check for the quantity of digits filled
1 #NUM    (N17) INIT <12345>
*
IF #NUM EQ MASK(NNNNNNNNNNNNNNNNN)
  WRITE 'Field has 17 positions.'
END-IF
  1. Using EXAMINE GIVING LENGTH, which returns only the total size (17)
1 #NUM        (N17) INIT <12345>
1 REDEFINE #NUM
   2 #NUM-A   (A17)
1 #TAMANHO    (N02)
*
EXAMINE FULL #NUM FOR ' ' GIVING LENGTH #TAMANHO
WRITE '=' #TAMANHO

Can anyone help me? Thanks in advance.

define data
local
1 #num (n17) init <12345>
1 #num-a (a) dynamic
1 #len (i4)
end-define
#num-a := #num
#len := *length(#num-a)
end

I believe if you actually move the Numeric to an Alpha and then EXAMINE FULL alpha for ‘0’ giving count that will solve your issue.

1 Like

Here is another option, using Math and the function LOG() (which is LOGe, not LOG10).

DEFINE DATA LOCAL                            
1 #N (N17) INIT <123456789112345>            
1 #LEN (N7)                                  
END-DEFINE                                   
*                                            
IF #N > 0                                    
  COMPUTE #LEN = LOG (#N) / 2.30258509299 + 1  /* n.b. LOG() is natural log, not base 10.
END-IF                                       
*                                            
DISPLAY #N #LEN                              
*                                            
END

This results in:

Page      1                 
                            
        #N           #LEN   
------------------ -------- 
                            
   123456789112345       15
1 Like

Isn’t it as simple as

IF  #NUM >= 10000000000000000    /* 16 zeros
  WRITE 'filled'
1 Like

Thank you! It worked :slight_smile:
I thought that just using an alphanumeric variable that is a redefinition of the numeric one would be enough.

This is a bit different, but I tested and worked too. Thank you!
I just don’t understand the calculation.

If Natural delivered a LOG10 calculation it would be simpler. For this explanation, I will use LOG for LOG10 and LN for LOGe.

LOG(n) answers the question, “what power of 10 is equal to n?” For example, LOG(100) = 2, 10^2 = 100. LOG(1000) = 3, LOG(10000) = 4, etc. You’ll notice that the log is always 1 less than the count of digits in the number.

Natural’s function is really LN(n), which answers the, “what power of e, where e=2.71828…, is equal to n?” There is a simple formula to get from LN(n) to LOG(n). LOG(n) = LN(n) / LN(10).

That probably doesn’t make it any clearer, but I’m 30+ years out from my math degree and I’m afraid I can’t explain it myself.

The calculation in Natural might be better without the hard-coded magic number:

COMPUTE #LEN = (LOG (#N) / LOG (10)) + 1

1 Like

Oh I get it! Thanks Jerome.

HI Jerome,
Well, according to your “math” LOG(10) is 1 (absolutely true, by the way :-); so, what’s the point to do LOG(N) / LOG(10)?
When you divide by 1, you don’t make any changes to your result, right? Puzzled :slight_smile:

Nikolay, Note that in the Natural code, LOG(10) is really LOGe(10) or LN(10) which is 2.30258509299…

Thank you, Jerome; well, I thought there were REALLY (and why not?) LOG (X) and LN (Y); I assume the next release of Natural will include LN as well as LOG :slight_smile: