利用 OS/400 MI 函數CvtEFN (Convert External Form to Numeric Value) 將文字性數字轉為數值
這個 CvtEFN (Convert External Form to Numeric Value) MI 函數是一個很強的函數,它轉換文字到數值,
實際上文字可包含 "$" "," "." 三個字元,而 CvtEFN 函數將刪除上述三個字元並產生一個數值。
File : QRPGLESRC
Member: STring
Type : RPGLE
* A leading currency symbol (defined by a mask)
* A sign symbol (+ or -), leading or trailing
* Digits (0-9)
* Commas to separate thousands
* A decimal point
* Leading and Trailing blanks
Source for service program String follows.
* ===================================================================
* = Service Program... String =
* = Description....... String routines =
* = =
* = Compile........... CrtRPGMod Module(YourLib/String) =
* = SrcFile(YourLib/YourSrcFile) =
* = CrtSrvPgm SrvPgm(YourLib/String) =
* = Export(*All) =
* ===================================================================
H NoMain
* ===================================================================
* = Prototypes =
* ===================================================================
* -------------------------------------------------------------------
* - CvtCharToNum - Convert character to numeric -
* - -
* - String Input -
* - Number Output -
* - -
* - Return value: Boolean success flag (0 = Success, 1 = Error ) -
* -------------------------------------------------------------------
D CvtCharToNum...
D PR N
D 40 Value
D 30 9
* -------------------------------------------------------------------
* - CvtExtFrmToNum - Convert external form to numeric value -
* - -
* - Receiver variable Output -
* - Receiver attributes Input -
* - Source string Input -
* - Source string length Input -
* - Symbol mask Input -
* -------------------------------------------------------------------
D CvtExtFrmToNum...
D PR ExtProc( '_CVTEFN' )
D * Value
D 7 Const
D * Value
D 10U 0 Const
D 3 Const
* ===================================================================
* = Procedure..... CvtCharToNum =
* = Description... Covert character to numeric =
* ===================================================================
P CvtCharToNum...
P B Export
D PI N
D String 40 Value
D Number 30 9
* -------------------------------------------------------------------
* - Data definitions -
* -------------------------------------------------------------------
D RcvAttr DS
D Type 1 Inz( X'03' )
D NbrLen 5I 0
D DecPos 3I 0 Overlay( NbrLen: 1 )
D TotDigits 3I 0 Overlay( NbrLen: 2 )
D 10I 0 Inz( *Zero )
D SymbolMask DS
D CurSymbol 1 Inz( '$' )
D ComSymbol 1 Inz( ',' )
D DecPntSymbol 1 Inz( '.' )
* -------------------------------------------------------------------
* - Convert character string to packed numeric format -
* -------------------------------------------------------------------
C Eval DecPos = %DecPos( Number )
C Eval TotDigits = %Len( Number )
C CallP CvtExtFrmToNum(
C %Addr( Number ) :
C RcvAttr :
C %Addr( String ) :
C %Len( %TrimR( String ) ) :
C SymbolMask
C )
C Return *Off
* -------------------------------------------------------------------
* - Return error condition for any error -
* -------------------------------------------------------------------
C *PSSR BegSr
C Return *On
C EndSr
P CvtCharToNum...
P E
File : QRPGLESRC
Member: STringEx
Type : RPGLE
* ===================================================================
* = Program....... StringEx =
* = Description... Example program using CvtCharToNum =
* = =
* = Compile........... CrtRPGMod Module(YourLib/StringEx) =
* = SrcFile(YourLib/YourSrcFile) =
* = CrtPgm Pgm(YourLib/StringEx) =
* = BndSrvPgm(YourLib/String) =
* ===================================================================
* ===================================================================
* = Prototypes =
* ===================================================================
* -------------------------------------------------------------------
* - CvtCharToNum - Convert character to numeric -
* - -
* - String Input -
* - Number Output -
* - -
* - Return value: Boolean success flag (0 = Success, 1 = Error ) -
* -------------------------------------------------------------------
D CvtCharToNum...
D PR N
D 40 Value
D 30 9
* ===================================================================
* = Definitions =
* ===================================================================
D MyString S 40
D MyNbr S 30 9
D RtnCode S N
* ===================================================================
* = Sample conversions =
* ===================================================================
C Eval MyString = '123,456,789.123456789'
C Eval RtnCode = CvtCharToNum(
C MyString :
C MyNbr
C )
C Eval MyString = '+123,456,789.123456789'
C Eval RtnCode = CvtCharToNum(
C MyString :
C MyNbr
C )
C Eval MyString = '-123,456,789.123456789'
C Eval RtnCode = CvtCharToNum(
C MyString :
C MyNbr
C )
C Eval MyString = '123,456,789.123456789+'
C Eval RtnCode = CvtCharToNum(
C MyString :
C MyNbr
C )
C Eval MyString = '123,456,789.123456789-'
C Eval RtnCode = CvtCharToNum(
C MyString :
C MyNbr
C )
C Eval MyString = '$123,456,789.123456789'
C Eval RtnCode = CvtCharToNum(
C MyString :
C MyNbr
C )
C Eval MyString = '$123,456,789.123456789+'
C Eval RtnCode = CvtCharToNum(
C MyString :
C MyNbr
C )
C Eval MyString = '$123,456,789.123456789-'
C Eval RtnCode = CvtCharToNum(
C MyString :
C MyNbr
C )
* -------------------------------------------------------------------
* - The following conversion fails (RtnCode = 1) due to an embedded -
* - blank -
* -------------------------------------------------------------------
C Eval MyString = '123, 56,789.123456789-'
C Eval RtnCode = CvtCharToNum(
C MyString :
C MyNbr
C )
C Eval *InLR = *On
A blog about IBM i (AS/400), MQ and other things developers or Admins need to know.
星期三, 11月 01, 2023
2002-02-02 利用 OS/400 MI 函數CvtEFN (Convert External Form to Numeric Value) 將文字性數字轉為數值
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言