Return to the RPG Tips
Character to Decimal
Here's an RPG/IV version using the equivalent _CVTEFN builtin: ** Valid source string symbols: ** One sign symbol . : - or + ** Decimal point . . : . ** Comma . . . . . . : , ** Blank . . . . . . : x'40 ** Digit . . . . . . : x'F0' - x'F9' ** Currency symbol as defined by mask ** D T_SIGNED c x'00' D T_FLOAT c x'01' D T_ZONED c x'02' D T_PACKED c x'03' D T_UNSIGNED c x'0A' ** D Mask Ds D CurSym 1a Inz( '$' ) D ComSym 1a Inz( ',' ) D DecPntSym 1a Inz( '.' ) ** D DPA_Template_T Ds D SclTyp 1a D RcvLen 5i 0 D DecPos 3i 0 Overlay( RcvLen: 1 ) D TotDig 3i 0 Overlay( RcvLen: 2 ) D Rsv 10i 0 Inz ** D CVTEFN Pr ExtProc( '_CVTEFN' ) D RcvVar * Value D RcvAtr Const Like( DPA_Template_T ) D Source * Value D SrcLen 10u 0 Const D SymMsk Const Like( Mask ) ** D Source s 25a Inz( '-12,345,678.912' ) D Packed s 15p 4 D Binary s 10i 0 ** **-- Zoned & Packed: C Eval SclTyp = T_PACKED C Eval DecPos = %DecPos( Packed ) C Eval TotDig = %Len( Packed ) ** C CallP CVTEFN( %Addr( Packed ) C : DPA_Template_T C : %Addr( Source ) C : %Len( %TrimR( Source )) C : Mask C ) **-- Binary & Float: C Eval SclTyp = T_SIGNED C Eval RcvLen = %Size( Binary ) ** C CallP CVTEFN( %Addr( Binary ) C : DPA_Template_T C : %Addr( Source ) C : %Len( %TrimR( Source )) C : Mask C ) ** C Return ** ============================================================================== ============================================================================== Here's an example of CVTEFN (using RPG calling the MI function, not an MI program). Indeed it seems a bit more than twice as fast as my routine. Even if I wrap CVTEFN in my own procedure, so it has a convenient "x = cvtnum (string)" interface, it's still almost twice as fast. My routine allows for more flexibility in the strings though (allowing parentheses to indicate negative, for example), and allowing either commas or blanks as separators. D cvtefn pr extproc('_CVTEFN') D rcvr * value D rcvrAttrs like(attrs) const D source 40a const D sourceLen 10u 0 const D mask 3a const D PACKED c x'03' D attrs ds D type 1a inz(PACKED) D length 5u 0 D decPos 1a overlay(length : 1) inz(x'09') D digits 1a overlay(length : 2) inz(x'1E') D reserved 10i 0 inz(0) C callp cvtefn (%addr(num) C : attrs C : string C : %len(string) C : '$,.') ================================================================================ ================================================================================ Here's another way (untested of course): 0464.00 * replace trailing blanks in amtA with leading zeros 0465.00 c eval amtA = %trim(amtA) 0466.00 c eval x = %scan(' ':amtA) 0467.00 c if x > 1 0468.00 c eval amtA = %subst('000000000000':1:13-x) 0469.00 c + %subst(amtA:1:x-1) 0470.00 c else 0471.00 c eval amtA = '0000000000.00' 0472.00 c endif
[report a broken link by clicking here]