Menu

Handling explicity currency and decimal point in COBOL

Emmad
2021-06-29
2021-06-29
  • Emmad

    Emmad - 2021-06-29

    I am learning how to best read numeric data exported from other software such as Excel. For example:
    Case 1: $00016.45
    Case 2: $-123.56
    Case 3:- $111.44
    Case4: $ 12.13
    Case5: $ -18.19

    In all cases, a dollar sign (currency) and a decimal point (say ".") must exist in the input.

    My question is, is there a way, without coding algorithms to handle any of the above formats in an input record?
    What PIC would that be? It is still OK if I had to pad the number with leading zeros if this would solve any of the cases.

    What about handling just the explicit decimal point wihtout the currency sign (say 12.34) without defining :
    01 Rec.
    02 fld.
    03 fld1 pic 99.
    03 f pic x
    03 fld2 pic 99.

    The question is about standard COBOL in general.

    Thank you.

     
    • Simon Sobisch

      Simon Sobisch - 2021-06-29

      Because of the -not being in the same place in your examples I'd likely do the following:

      first: validate the dollar sign, if needed (check that you have exactly one $ and before either nothing or a +/-)

      Then for portable COBOL:

      MOVE INPUT-REC  TO TMP-REC
      INSPECT TMP-REC REPLACING ALL '$' BY SPACE
      MOVE FUNCTION NUMVAL(TMP-REC) TO NUMERIC-VAR
      

      and for COBOL 202x or GnuCOBOL:

      MOVE FUNCTION NUMVAL(
             FUNCTION SUBSTITUTE(INPUT-REC, '$', ' ')
             )
        TO NUMERIC-VAR
      
       
      👍
      1
    • Emmad

      Emmad - 2021-06-29

      Thank you for the immediate and accurate response.
      You are superb indeed.
      Stay safe.

       
      👍
      1

Anonymous
Anonymous

Add attachments
Cancel