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:
- 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.'
- 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.
1 #num (n17) init <12345>
1 #num-a (a) dynamic
1 #len (i4)
#num-a := #num
#len := *length(#num-a)
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.
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)
IF #N > 0
COMPUTE #LEN = LOG (#N) / 2.30258509299 + 1 /* n.b. LOG() is natural log, not base 10.
DISPLAY #N #LEN
This results in:
Isn’t it as simple as
IF #NUM >= 10000000000000000 /* 16 zeros
Thank you! It worked
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
Oh I get it! Thanks 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
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