×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Question about numeric operands
2

Question about numeric operands

Question about numeric operands

(OP)
Hi guys,

Firstly, Thank you very much for reading and hopefully answering my question. I'm in my first COBOL I class at college and to be frank, i'm floating around hoping i'm on the right track.

My questions is I have a string of numbers in my input file:
028238.24

However, when I compile it comes up with an error for a section of the file that uses that specific spot:
Operand must be an arithmetic expression
as well as  Operand must be a numeric data-name or a numeric literal.

I've read some of the question on this forum already and they haven't helped me much, so i'm hoping a more specific question will do the trick.

What I want displayed 28,238.24   I would like to have commas as well as the 0 to be purged. I tried to do a numeric literal but it is giving me these errors.

05  CM-BEGINNING-BALANCE        PIC 9(7)V99.
Is my routine to pull it from the input file.

and my Customer-Line (What i want to be printed, basically) is coded as:
05   CL-BEGINNING-BALANCE PIC ZZZ,ZZ9.99.

Thank you very much.


 

RE: Question about numeric operands

If the number appears as "028238.24" in the file, then the period is not a numeric value consistent a PIC 9 field.  Furthermore, you have an intended format of PIC 9(6)V99 which is unworkable as well.

I don't know what compiler you are on (that matters), but I would try either a literal "." in the input definition (it may or may not work), or barring that, processing to force the "." out of the input since it is not a numeric.

As a hint, a proper PIC 9(7)V99 appears in input as "002823824" (the V is an implied decimal and not a literal decimal).

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.

RE: Question about numeric operands

(OP)
Glenn, Thank you very much for your post. It seemed to make sense, but after trying everything (to my knowledge, atleast) I haven't been able to convert it.

I changed the input file to not use any decimals at all.

It seems like i'm running out of options. What would you try?

I'm just using PuTTy to connect to my school and editing it through there. So i'm not entirely sure.

Thank you very much.

RE: Question about numeric operands

For this being a "first COBOL class", your best option is probably your instructor.  For being a "first COBOL class" as well, removing the period is (I'm guessing) probably above your pay grade.  So you would do well to check with your instructor to clarify on how you need to be making your test files for your assignments, since it seems you are uncertain on what is required.

That being said, I'm sure simple questions are okay here, but helping too much on homework would be a TOS violation here.  So please keep that in mind if you post any more questions.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.

RE: Question about numeric operands

(OP)
Thanks anyways Glenn. Was just hoping if someone could help correct the error. I've met with my instructor several times and no avail.

As said, Thank you anyways.

RE: Question about numeric operands

1.  What is the error message?
2.  What compiler and o/s are you using?
3.  What is the source of the file?

RE: Question about numeric operands

You can write a procedure which tries to convert the given string into a number according the rules you define.
Here is a simple example:

CODE

       IDENTIFICATION DIVISION.            
       PROGRAM-ID. STR2NUM.                
       ENVIRONMENT DIVISION.               
       CONFIGURATION SECTION.              
       SOURCE-COMPUTER. IBM-ISERIES.       
       OBJECT-COMPUTER. IBM-ISERIES.       
       SPECIAL-NAMES.                      
       DATA DIVISION.                      
       WORKING-STORAGE SECTION.            
       01 STR-LEN9    PIC X(9).            
       01 STR-DEC6V2 REDEFINES STR-LEN9.   
          05 BEFORE-POINT PIC X(6).        
          05 FILLER       PIC X.           
          05 AFTER-POINT  PIC X(2).        
       01 DEC6        PIC 9(6).            
       01 DEC2        PIC 9(2).
       WORKING-STORAGE SECTION.                                     
       01 STR-LEN9    PIC X(9).                                     
       01 STR-DEC6V2 REDEFINES STR-LEN9.                            
          05 BEFORE-POINT PIC X(6).                                 
          05 FILLER       PIC X.                                    
          05 AFTER-POINT  PIC X(2).                                 
       01 DEC6        PIC 9(6).                                     
       01 DEC2        PIC 9(2).                                     
       01 DEC6V2      PIC 9(6)V9(2).                                
       01 DEC6V2-DSP  PIC ZZZ,ZZ9.99.                               
      *                                                             
       01 MY-STRING1     PIC X(9) VALUE "028238.24".                
       01 MY-STRING2     PIC X(9) VALUE "  123.245".                
       PROCEDURE DIVISION.
       MAIN.
      *    Trying it with MY-STRING1                                
           DISPLAY "Trying to convert the string '" MY-STRING1 "'".
           INITIALIZE DEC6V2                                        
           MOVE MY-STRING1 TO STR-LEN9
           PERFORM PROC-STR2NUM                                     
           MOVE DEC6V2 TO DEC6V2-DSP.                               
           DISPLAY "The number is " DEC6V2-DSP                      
                                                                    
      *    Trying it with MY-STRING2                                
           DISPLAY "Trying to convert the string '" MY-STRING2 "'".
           INITIALIZE DEC6V2                                        
           MOVE MY-STRING2 TO STR-LEN9        
           PERFORM PROC-STR2NUM                
           MOVE DEC6V2 TO DEC6V2-DSP.          
           DISPLAY "The number is " DEC6V2-DSP
      *                                        
           GOBACK                              
           .                                   
                                               
        PROC-STR2NUM.                          
            IF BEFORE-POINT IS NUMERIC AND     
               AFTER-POINT  IS NUMERIC         
              MOVE BEFORE-POINT TO DEC6          
              MOVE AFTER-POINT  TO DEC2          
              COMPUTE DEC6V2 = DEC6 + DEC2 / 100
           ELSE                                  
              DISPLAY "This doesn't work !"      
           END-IF                                
           .                                                
Output:

CODE

Trying to convert the string '028238.24'            
The number is  28,238.24                            
Trying to convert the string '  123.245'            
This doesn't work !                                 
The number is       0.00                            
 

RE: Question about numeric operands

The source above has bug - 2x working storage.
Here is the correct source:

CODE

       IDENTIFICATION DIVISION.
       PROGRAM-ID. STR2NUM.
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SOURCE-COMPUTER. IBM-ISERIES.
       OBJECT-COMPUTER. IBM-ISERIES.
       SPECIAL-NAMES.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 STR-LEN9    PIC X(9).
       01 STR-DEC6V2 REDEFINES STR-LEN9.
          05 BEFORE-POINT PIC X(6).
          05 FILLER       PIC X.
          05 AFTER-POINT  PIC X(2).
       01 DEC6        PIC 9(6).
       01 DEC2        PIC 9(2).
       01 DEC6V2      PIC 9(6)V9(2).
       01 DEC6V2-DSP  PIC ZZZ,ZZ9.99.
      *
       01 MY-STRING1     PIC X(9) VALUE "028238.24".
       01 MY-STRING2     PIC X(9) VALUE "  123.245".
       PROCEDURE DIVISION.
       MAIN.
      *    Trying it with MY-STRING1
           DISPLAY "Trying to convert the string '" MY-STRING1 "'".
           INITIALIZE DEC6V2
           MOVE MY-STRING1 TO STR-LEN9
           PERFORM PROC-STR2NUM
           MOVE DEC6V2 TO DEC6V2-DSP.
           DISPLAY "The number is " DEC6V2-DSP

      *    Trying it with MY-STRING2
           DISPLAY "Trying to convert the string '" MY-STRING2 "'".
           INITIALIZE DEC6V2
           MOVE MY-STRING2 TO STR-LEN9
           PERFORM PROC-STR2NUM
           MOVE DEC6V2 TO DEC6V2-DSP.
           DISPLAY "The number is " DEC6V2-DSP
      *
           GOBACK
           .

       PROC-STR2NUM.
           IF BEFORE-POINT IS NUMERIC AND
              AFTER-POINT  IS NUMERIC
              MOVE BEFORE-POINT TO DEC6
              MOVE AFTER-POINT  TO DEC2
              COMPUTE DEC6V2 = DEC6 + DEC2 / 100
           ELSE
              DISPLAY "This doesn't work !"
           END-IF
           .

RE: Question about numeric operands

COBOL is being taught in college? I didn't know that.

RE: Question about numeric operands

It should be.  COBOL is a modern language and the only one that supports many accounting and other business concerns, such as working with 16 (now 31) digit numbers with no rounding errors, as well as code that can be understood by non-programmer auditors and users.

RE: Question about numeric operands

No flame, but I cannot agree with "COBOL is a modern language" smile
OK it has now more intrinsic functions and on some platforms greate enhancements like the statements XML PARSE and XML GENERATE,...but I still miss at least local variables.
I had an occasion to work with ABAP - only one week. ABAP is IMHO an improved COBOL. But I found in ABAP everything I missed in COBOL i.e: local variables and recursion. I know you can declare now in some COBOL dialects, that program should be recursive

CODE

       IDENTIFICATION DIVISION.
       PROGRAM-ID. FACTORIAL RECURSIVE.
       ....
but I mean a recursion at paragraph level, something like this (in ABAP paragraph is marked with the keyword FORM):

CODE --> ABAP

* Form with recursive call for computation of factorial
FORM faktorial USING value(n) TYPE i CHANGING fak TYPE f.
* local variables
  DATA: n_1 TYPE i,
        fak_n_1 TYPE f.

  IF n <= 0.
    fak = 1.
  ELSE.
    n_1 = n - 1.
    PERFORM faktorial USING n_1 CHANGING fak_n_1.
    fak = n * fak_n_1.
  ENDIF.
ENDFORM.
If it could be possible to enhance COBOL in the direction of ABAP, so that it supports at least local variables and maybe recursion, then it would be "modern language" for the next 50 years.

RE: Question about numeric operands

Yes, not all versions support local variables and recursion.  The problem with recusion at the paragraph level is the whole design structure of COBOL where the variables must be declared at the beginning of the program.  However, the version I use at least, allows embedded subprograms which can be of any size.  And, of course, local variables are supported.

But is there any other high-level language that supports 31-digit fixed-point decimal arithmetic?

RE: Question about numeric operands

Quote (webrabbit):


However, the version I use at least, allows embedded subprograms which can be of any size.  And, of course, local variables are supported.
And can you say what compiler do you use?

I'm using ILE COBOL on iSeries. It supports procedures and recursion, but using this features requires multiple compilation/binding steps - see this thread from COBOL/400 mailing list
http://archive.midrange.com/cobol400-l/200512/msg00024.html

Quote (webrabbit):


But is there any other high-level language that supports 31-digit fixed-point decimal arithmetic?
Not so far from COBOL and mainframe: REXX has decimal arithmetic with arbitrary precision, which you can set/change during computation.
For example this code

CODE --> REXX

rslt = 1/6
say "1/6 with" DIGITS() "digits precission:"
rslt = 1/6
say rslt
say

NUMERIC DIGITS 31
rslt = 1/6
say "1/6 with" DIGITS() "digits precission:"
rslt = 1/6
say rslt
say

NUMERIC DIGITS 66
say "1/6 with" DIGITS() "digits precission:"
rslt = 1/6
say rslt
computes

CODE

1/6 with 9 digits precission:
0.166666667

1/6 with 31 digits precission:
0.1666666666666666666666666666667

1/6 with 66 digits precission:
0.166666666666666666666666666666666666666666666666666666666666666667
 

RE: Question about numeric operands

webrabbit,

I didn't want to imply that it shouldn't be taught. I am surprised that it is. I cut my programming teeth on COBOL and RPG along with a little FORTRAN and BASIC.

RE: Question about numeric operands

FYI: Micro Focus COBOL Version 3.2.46 (1994, very old)

RE: Question about numeric operands

I do recursion and use local variables with an even older compiler (on Unix):
Micro Focus cobol v3.1.31-Be

RE: Question about numeric operands

This should be a simple problem given that this is an introductory COBOL course.  The compiler involved likely is at least 1985 standard compliant which means it should support de-edited moves:

CODE

01  WS-INPUT-NUMBER    PIC 9(6).99.
01  WS-OUTPUT-NUMBER   PIC ZZZ,ZZ9.99.
...
MOVE WS-INPUT-NUMBER TO WS-OUTPUT-NUMBER
That should do the trick.

Glenn

RE: Question about numeric operands

I thought that the question was about converting the content of string into number (if possible), i.e
from

CODE

01  WS-INPUT-NUMBER    PIC X(9).
to

CODE

01  WS-OUTPUT-NUMBER   PIC ZZZ,ZZ9.99.
where this

CODE

MOVE WS-INPUT-NUMBER TO WS-OUTPUT-NUMBER
doesn't work.
  

RE: Question about numeric operands

Quote:


I thought that the question was about converting the content of string into number (if possible), i.e

No.  Homework question.  I gather that the OP didn't understand the course problem well enough to generate a test data file properly (or got the format wrong entirely).  His/her instructor is the best person that they need to be dealing with for this problem.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.

RE: Question about numeric operands

Yes I forgot, that now the intrinsic function NUMVAL is available smile
Maybe it's depending on compiler, but in my case this simple move into display variable doesn't work:

CODE

MOVE FUNCTION NUMVAL(CM-BEGINNING-BALANCE) TO CL-BEGINNING-BALANCE
Trying to do it, I got this error:

CODE

 *    17  MSGID: LNC2780  SEVERITY: 30  SEQNBR:  000128                        
          Message . . . . :   Numeric function 'FUNCTION NUMVAL' was found, but
            was not allowed in this context. Statement discarded.              
I have first to compute the value of NUMVAL into numeric variable and then move it into numeric edited variable:

CODE

       IDENTIFICATION DIVISION.
       PROGRAM-ID. STR2NUM2.
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SOURCE-COMPUTER. IBM-ISERIES.
       OBJECT-COMPUTER. IBM-ISERIES.
       SPECIAL-NAMES.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 DEC6V2         PIC 9(6)V9(2).
       01 DEC6V2-DSP     PIC ZZZ,ZZ9.99.
      *
       01 MY-STRING1     PIC X(9) VALUE "028238.24".
       01 MY-STRING2     PIC X(9) VALUE "  123.245".
       PROCEDURE DIVISION.
       MAIN.
      *    Trying it with MY-STRING1
           DISPLAY "Trying to convert the string '" MY-STRING1 "'".
           INITIALIZE DEC6V2
           COMPUTE DEC6V2 = FUNCTION NUMVAL(MY-STRING1)
           MOVE DEC6V2 TO DEC6V2-DSP
           DISPLAY "The number is " DEC6V2-DSP

      *    Trying it with MY-STRING2
           DISPLAY "Trying to convert the string '" MY-STRING2 "'".
           INITIALIZE DEC6V2
           COMPUTE DEC6V2 = FUNCTION NUMVAL(MY-STRING2)
           MOVE DEC6V2 TO DEC6V2-DSP
           DISPLAY "The number is " DEC6V2-DSP
      *
           GOBACK
           .
Using the intrinsic function NUMVAL is simpler than writing an own converting procedure and works better:

CODE

> call STR2NUM2                           
  Trying to convert the string '028238.24'
  The number is  28,238.24                
  Trying to convert the string '  123.245'
  The number is     123.25                

RE: Question about numeric operands

Hi,

You can try this;

Define your record field as:

PIC 9(6).99.  (use your orig value - 028238.24)   

Code the receiving field as: PIC ZZZ,ZZ9.99.

Then, MOVE rec-fld TO recv-fld

I haven't tested, but the manual says num-ed to num-ed is permissable.

Regards, Jack.

"A problem well stated is a problem half solved" -- Charles F. Kettering
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close