Search code examples
cobolfixed-pointgnucobol

Reading floating-point numbers from file in COBOL


I have fixed-point numbers in file, one in each line, in this format S9(6)V9(2) but when they are actually read, I'm getting non numeric errors while trying to put them in math operations. What is more, when I try to display them in program, a number that is written in file as 567123.45 is saved in variable as +567123.04. And for example the number from file 123.45 is saved in variable as +123.45.00 and it provokes the following error 'WS-VALUE' not numeric: '123.45 0' during a math operation. Why is that? I'm using OpenCobolIDE 4.7.4 for Windows.

EDIT: File has records of the following form separated by new lines (read by READ operation record after record):

  01 WS-OPERATION.
     05 WS-ID PIC A(2).
     05 WS-CLIENT PIC 9(5).
     05 WS-COUNTRY PIC A(4).
     05 WS-VALUE PIC S9(6)V9(2). 

Solution

  • The reason is that you try to un-edit a field. 567123.45 in the data is not conforming to PIC S9(6)V9(2) but to -9(6).9(2). - internal stored data vs. print-data.

    Simply changing the definition and use MOVE WS-VALUE TO WS-VALUE-INTERNAL (which is defined like you want to) may work with a specific compiler (and specific data) but I'd go a different route:

    I'd suggest to always validate the data before doing something with it (the file may be broken or external edited). At least check the simple numeric data like WS-CLIENT for IS NUMERIC and either do a full validation on the data field WS-VALUE or at least use MOVE FUNCTION NUMVAL(WS-VALUE) TO WS-VALUE-INTERNAL.