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

Numeric fields in COBOL 1

Status
Not open for further replies.

stevenbyers

Programmer
Jan 12, 2006
4
US
How do I define a field in a COBOL program that can read data that has a floating and/or stationary negative sign (-). For example, if the data I want to read is on a file as:

- 500
-25

What PICTURE clause(s) should I use?
 
Steven,

This is a classic issue. In order for this forum to give its best advice, could you please tell us what computing platform and compiler you are using? Thanks.

Tom Morrison
 
Tom,

We are on an HP 5430 with a Unix operating system. When I compile my program, the following line appears (among others):

Micro Focus Server Express V2.2 revision 000 Compiler

I assume that means I'm using a Micro Focus compiler?
Thanx for your help.

Stev
 
On the assumption that what you are interested in is the numeric value of the string, I would suggest that you investigate the NUMVAL function (section 11.9.36 in the Server Express Language Reference Manual). The picture you would use would be X(n) (where n is the number of character positions in the input field).

Tom Morrison
 
Thank you. Thank you. Thank you. The function worked like a charm.
 
I use old special routine that will work at any Cobol system, (even old).
Code:
000001 IDENTIFICATION DIVISION.
000002 PROGRAM-ID. NUMSPL.
000003 ENVIRONMENT DIVISION.
000004 DATA DIVISION.
000005 WORKING-STORAGE SECTION.
000006 01  I            PIC S9(5)  COMP-3 VALUE 0.
000007 01  J            PIC 9(4)   COMP-6 VALUE 0.
000008 01  K            PIC 9(4)   COMP-6 VALUE 0.
000009 01  N1           PIC 9(13).
000010 01  N1-X REDEFINES N1.
000011     02 VEC-N1    PIC X OCCURS 13.
000012 01  N2           PIC V9(5).
000013 01  N2-X REDEFINES N2.
000014     02 VEC-N2    PIC X OCCURS 5.
000015 01  X            PIC X(20) JUST RIGHT.
000016 01  X-R REDEFINES X.
000017     02 VEC-X     PIC X OCCURS 20.
000018 01  SW-DOT       PIC 9.
000019 01  SW-SIGN      PIC 9.
000020**********************
000021 LINKAGE SECTION.
000022 01  INP          PIC X(20) JUST RIGHT.
000023 01  N            PIC S9(12)V9(5) COMP-3.
000024********************************************************
000025 PROCEDURE DIVISION USING INP N.
000026 MAIN SECTION.
000027 1.  MOVE INP TO X.
000028     PERFORM IN-NUM.
000029     EXIT PROGRAM.
000030 IN-NUM.
000031     IF VEC-X(20) = SPACE MOVE "," TO VEC-X(20).
000032     INSPECT X REPLACING ALL SPACE BY ZERO.
000033     MOVE 0 TO N1 N2.
000034     MOVE 0 TO K SW-DOT SW-SIGN.
000035     PERFORM IN-NUM1 VARYING I FROM 1 BY 1 UNTIL I > 20.
000036     IF SW-DOT > 0 SUBTRACT 1 FROM K
000037              ELSE MOVE 20 TO K.
000038     MOVE 14 TO J.
000039     PERFORM IN-NUM2
000040                  VARYING I FROM K BY -1 UNTIL (J = 1 OR I < 1).
000041     IF SW-DOT > 0 ADD 2 TO K
000042                   MOVE 0 TO J
000043                   PERFORM IN-NUM3
000044                   VARYING I FROM K BY 1 UNTIL (J = 5 OR I > 20).
000045     ADD N1 N2 GIVING N.
000046     IF SW-SIGN = 1 MULTIPLY -1 BY N.
000047    IN-NUM1.
000048     IF VEC-X(I) = "." MOVE I TO K
000049                       MOVE 77 TO I
000050                       MOVE 1 TO SW-DOT.
000051    IN-NUM2.
000052     IF VEC-X(I) = "-" MOVE 1 TO SW-SIGN
000053      ELSE IF VEC-X(I) NOT = "," SUBTRACT 1 FROM J
000054                                 MOVE VEC-X(I) TO VEC-N1(J).
000055    IN-NUM3.
000056     IF VEC-X(I) = "-" MOVE 1 TO SW-SIGN
000057      ELSE IF VEC-X(I) NOT = "," ADD 1 TO J
000058                                 MOVE VEC-X(I) TO VEC-N2(J).
000059 EX-IN-NUM.

Baruch
 
If your input is perhaps not numeric, the function NUMVAL or NUMVAL-C is perhaps not helping you. Try it out with several kinds of input you expect. If it does not work well, you need an other approach.

Also functions are from a library. It can be that they don't perform very fast compared to an efficient coded approach without functions.
 
In acucobol you would just treat the input as an alpha filed and move it to a mumeric field with conversion:

MOVE [alpha] TO [numeric] CONVERT.
 
The best way is not to accept stupid values.

If you do not like my post feel free to point out your opinion or my errors.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top