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

Character Field Value To Numeric Field

Status
Not open for further replies.

Skittle

ISP
Sep 10, 2002
1,528
US
Ever since I started working with RPG I have moved a chracter field value to a numeric field via the foloowing method. Is there a better way to do it now we have free format version 5 RPG IV?

*
*
* Convert Char To Number
DD1 DS
DD1CHAR 1 2
DD1NO 1 2 0
*
* Will Give Us 33
DW1RESULT 2 0

/FREE
EVAL D1CHAR = '23';
EVAL RESULT = 10 + D1NO; // Result = 33 //
/END-FREE
 
IBM's Barbara Morris wrote this function, and I am using it under V5R1. The nice part is that it won't blow up if the character string is blank; it just returns zero. Plus, it will return a sign.



"When once you have tasted flight, you will forever walk the Earth with your eyes turned skyward, for here you have been, and there you will always long to return."

--Leonardo da Vinci
 
The way I'm doing this in RPG free form is this.
This subprocedure is passed a character dollar amount, then converts that into a numeric value.

Code:
     P ConvertDollar   B
      //------------------------------------------------------------------------
     D ConvertDollar   PI             8S 2
     D   CharValue                   11    Value
     D
     D NegativeAmt     S               N   Inz(*Off)
     D CharDollar      Ds
     D  DollarAmt                     8S 2
      //------------------------------------------------------------------------
      /Free

       If %Scan('-':CharValue) > 0;
         NegativeAmt = *On;
         EvalR CharValue = %TrimR(%Replace('':CharValue:
                             %Scan('-':CharValue):1));
       Else;
         NegativeAmt = *Off;
       EndIf;
       DoW %Scan(',':CharValue) > 0;
         EvalR CharValue = %TrimR(%Replace('':CharValue:
                              %Scan(',':CharValue):1));
       EndDo;
       EvalR CharValue = %TrimR(%Replace('':CharValue:%Scan('.':CharValue):1));
       EvalR CharDollar = %TrimR(CharValue);
       If NegativeAmt;
         DollarAmt = DollarAmt * -1;
       EndIf;
       Return DollarAmt;

      /End-Free

Note the chardollar data structure. Once I've cleaned up all of the special characters, I simply pass the character value to the data structure, the data structure will then convert the char value into a signed numeric value.

RedMage1967
IBM Certifed - RPG IV Progammer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top