HI FRIENDS,
CAN ANYBODY TELL ME HOW CAN WE PROGRAMATICALLY KNOW THE NUMBER OF DIGITS A NUMBER HAS...FOR EG IF THE USER ENTERS 234 THE OUTPUT SHOULD BE 3 DIGITS AND SO ON.....
hi kurup....
i think we can get it easily like this
just check whether it is <10 then digits = 1
<100 then digits = 2 and so on..
simple isnt it??
bk deep
Your ques needs a little more detail. For example can the "user" enter "000123" or
" 123" or " 123 ", or "0012.3". How is the field defined, e.g. pic xxx, pic 999, etc? Do you want to know significant digits or all digits, for example if "user" entered " 123" there are 6 digits (or positions; one can argue spaces are not digits, but that should be made clear). Well you get the idea.
To take the simplest case (a mistake when dealing w/what a user my enter):
"User" enters 123 into a 6 byte pic 9 fld.
You can code:
perform
varying fpos from +1 by +1
until pic9-fld(fpos:1) not = zero
if pic9-fld = zero
add +1 to f0-cnt
end-if
end-perform
compute true-len = (length of pic9-fld - fpos) + 1
Remember this is the simplest case. How would you do it if the "user" I/P had to be a decimal amt, e.g. 1.23? Could you use pic9? What if 12300 were entered?, etc.
Some fairly comlicated editing routines have been written to allow maximum flexibility in entering numeric data and at the same time providing strict I/P validity, e.g.:
accepting $1.23, +1.23, 1.23-, 1.23CR, " + 1.23 ", etc.
rejecting 1.2A, "1 .23", $$1.23, etc.
Generally, these edit routines use pic x for numeric flds to allow the flexibility.
After using the edit subrutine, the pgmmer can the add his own "reasonability" edits, dictated by the application requirements, e.g.:
0 not valid
123.00 too large an amt
1.23 negative not a valid amt
etc.
Here is a snipet of code I use to test for valid digits and count the number of digits prior to hitting a non-digit value. The routine uses "reference modification" to scan the input field.
ADD 1 TO ZERO GIVING IX-1
MOVE 'U' TO DIGIT-FLAG
PERFORM UNTIL IX-1 > STOP-1
OR DIGIT-FLAG = 'N'
IF FIELD-AS-ALPHAMERIC(IX-1:1) < 0
OR FIELD-AS-ALPHAMERIC(IX-1:1) > 9
MOVE 'N' to DIGIT-FLAG
END-IF
IF DIGIT-FLAG = 'U'
ADD 1 TO IX-1
END-IF
END-PERFORM
A sample program can be easily downloaded from the following URL...
slade...
thank u for giving me a detailed description.I meant only numbers without spaces and sign characters with it.I think deep's idea will work out in that case.However i will check for conditions u have mentioned.
and saginaw....
u are using reference modification and trying to achieve it..good idea indeed thanku also
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.