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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NUMBER OF DIGITS IN A NUMBER

Status
Not open for further replies.

kurup

Programmer
Jun 1, 2001
32
IN
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.....

REGARDS
KURUP
 
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
 
Hi Kurup,

Your ques needs a little more detail. For example can the &quot;user&quot; enter &quot;000123&quot; or
&quot; 123&quot; or &quot; 123 &quot;, or &quot;0012.3&quot;. 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 &quot;user&quot; entered &quot; 123&quot; 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):

&quot;User&quot; 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 &quot;user&quot; 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, &quot; + 1.23 &quot;, etc.
rejecting 1.2A, &quot;1 .23&quot;, $$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 &quot;reasonability&quot; 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.

Regards, Jack.


Regards, Jack.
 
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 &quot;reference modification&quot; 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...

http:\\
Good Luck,
Saginaw
Saginaw@simotime.com
 
bk deep..
got it..thanku

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

regards

kurup

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top