Skip to main content

Problem:

How can you convert numeric data coming in from a csv into COBOL numerical values ?

If you have a CSV file then the numeric data is in text format. These need to be converted in COBOL data items such as s9(9)v99.

Resolution:

You can write routines to perform the conversion but there is also a  Intrinsic function numval (and  numval-c) that will perform this conversion.

Some example code that does this is:-

       special-names.

           class decimalchar is "0" through "9" " " "-" "."

           .

       working-storage section.

       01  ws-text1          pic x(20)  value "-123.34".

       01  ws-text2          pic x(20)  value "123.34".

       01  ws-text3          pic x(20)  value "0.5456".

       01  ws-text4          pic x(20)  value "aabc".

       01  ws-numeric        pic s9(5)v99.

       01  ws-edited         pic 9(5).99.

       01  ws-testfield      pic x(20).

       01  ws-testflag       pic x.

           88  is-valid              value "1".

           88  is-invalid            value "2".

       01  ws-sub            pic 9(9) comp-5.

       procedure division.

       start-section section.

           move ws-text1 to ws-testfield

           perform validate-number

           if is-valid

               display "ws-text1 is valid"

           else

               display "ws-text1 is invalid"

           end-if

           move ws-text2 to ws-testfield

           perform validate-number

           if is-valid

               display "ws-text2 is valid"

           else

               display "ws-text2 is invalid"

           end-if

           move ws-text3 to ws-testfield

           perform validate-number

           if is-valid

               display "ws-text3 is valid"

           else

               display "ws-text3 is invalid"

           end-if

           move ws-text4 to ws-testfield

           perform validate-number

           if is-valid

               display "ws-text4 is valid"

           else

               display "ws-text4 is invalid"

           end-if

      ***** Now show results from numval function.

           move function numval(ws-text1) to ws-numeric

           move function numval(ws-text1) to ws-edited

           display "Testing -->" ws-text1

           display "  ws-numeric -->" ws-numeric

           display "  ws-edited  -->" ws-edited

           move function numval(ws-text2) to ws-numeric

           move function numval(ws-text2) to ws-edited

           display "Testing -->" ws-text2

           display "  ws-numeric -->" ws-numeric

           display "  ws-edited  -->" ws-edited

           move function numval(ws-text3) to ws-numeric

           move function numval(ws-text3) to ws-edited

           display "Testing -->" ws-text3

           display "  ws-numeric -->" ws-numeric

           display "  ws-edited  -->" ws-edited

           move function numval(ws-text4) to ws-numeric

           move function numval(ws-text4) to ws-edited

           display "Testing -->" ws-text4

           display "  ws-numeric -->" ws-numeric

           display "  ws-edited  -->" ws-edited

           stop run.

       validate-number section.

      *

      ****** Basic Test for valid numeric data

      *

           set is-valid to true

           perform varying ws-sub from 1 by 1

           until ws-sub > length of ws-testfield

               if ws-testfield(ws-sub:1) = space

                   if ws-testfield(ws-sub:) not = spaces

                       set is-invalid to true

                   else

                       set is-valid to true

                   end-if

                   exit section

               end-if

               if ws-testfield(ws-sub:1) not decimalchar

                   set is-invalid to true

                   exit section

               end-if

           end-perform

           .

A working Net Express project is attached to this Article.

Attachments:

TextNumerics.zip

Old KB# 1400