自己寫的擷取小數位的函式

TolyHuang發表於2009-04-28
用SE37建立擷取小數位的函式:'ZFN_GET_ROUND [@more@]

使用方法:
CALL FUNCTION 'ZFN_GET_ROUND'
EXPORTING
PI_VALUE = "要處理的資料
PI_LEN = "要載取的小數點後面的位數
PI_TYPE = 'A' "載取方法:A 四捨五入 B 捨去後面的數,不處理進位問題 C 後面一位衹要大於0都要進位 
IMPORTING
PO_VALUE = . "返回的最後結果

實現原始碼:

FUNCTION ZFN_PB_W_0114.
*"----------------------------------------------------------------------
*"*"區域介面:
*" IMPORTING
*" REFERENCE(PI_VALUE)
*" REFERENCE(PI_LEN) TYPE I
*" REFERENCE(PI_TYPE) TYPE CHAR1 DEFAULT 'A'
*" EXPORTING
*" REFERENCE(PO_VALUE)
*"----------------------------------------------------------------------

DATA: TMP_VALUE TYPE STRING,
TMP_INT TYPE STRING,
TMP_SCALE TYPE STRING.
DATA: TMP_LAST TYPE C LENGTH 1,
TMP_VAL TYPE I,
TMP_LVAL TYPE I.

* BREAK-POINT 'GC_JIMHUANG'.
* IF PI_VALUE = 0. PO_VALUE = 0 . EXIT. ENDIF.
TMP_VALUE = PI_VALUE.
CONDENSE TMP_VALUE.
SPLIT TMP_VALUE AT '.' INTO: TMP_INT TMP_SCALE.
CONDENSE: TMP_INT,TMP_SCALE.

IF TMP_SCALE = ''. PO_VALUE = PI_VALUE. EXIT. ENDIF.

CASE PI_TYPE.
WHEN 'A'. " 四捨五入
TMP_VAL = TMP_SCALE+0(PI_LEN).
TMP_LAST = TMP_SCALE+PI_LEN(1).
IF TMP_LAST = ''. TMP_LAST = '0'. ENDIF.
TMP_LVAL = TMP_LAST.
IF TMP_LVAL >= 5.
TMP_VAL = TMP_VAL + 1.
ENDIF.
TMP_SCALE = TMP_VAL.
CONDENSE TMP_SCALE.

IF STRLEN( TMP_SCALE ) > PI_LEN.
TMP_SCALE = TMP_SCALE+1(PI_LEN).
TMP_VAL = TMP_INT.
TMP_VAL = TMP_VAL + 1.
TMP_INT = TMP_VAL.
CONDENSE TMP_INT.
ENDIF.

DO PI_LEN TIMES.
IF STRLEN( TMP_SCALE ) < PI_LEN.
CONCATENATE '0' TMP_SCALE INTO TMP_SCALE.
ELSE.
EXIT.
ENDIF.
ENDDO.

CONCATENATE TMP_INT '.' TMP_SCALE INTO TMP_VALUE .
CONDENSE TMP_VALUE.

PO_VALUE = TMP_VALUE.


WHEN 'B'. "捨去後面的數,不處理進位問題
TMP_SCALE = TMP_SCALE+0(PI_LEN).
CONDENSE TMP_SCALE.
CONCATENATE TMP_INT '.' TMP_SCALE INTO TMP_VALUE .
CONDENSE TMP_VALUE.

PO_VALUE = TMP_VALUE.

WHEN 'C'. "後面一位衹要大於0都要進位
* BREAK-POINT 'GC_JIMHUANG'.
TMP_VAL = TMP_SCALE+0(PI_LEN).
TMP_LAST = TMP_SCALE+PI_LEN(1).
IF TMP_LAST <> '0' AND TMP_LAST <> ''.
TMP_VAL = TMP_VAL + 1.
ENDIF.
TMP_SCALE = TMP_VAL.
CONDENSE TMP_SCALE.

IF STRLEN( TMP_SCALE ) > PI_LEN.
TMP_SCALE = TMP_SCALE+1(PI_LEN).
TMP_VAL = TMP_INT.
TMP_VAL = TMP_VAL + 1.
TMP_INT = TMP_VAL.
CONDENSE TMP_INT.
ENDIF.

DO PI_LEN TIMES.
IF STRLEN( TMP_SCALE ) < PI_LEN.
CONCATENATE '0' TMP_SCALE INTO TMP_SCALE.
ELSE.
EXIT.
ENDIF.
ENDDO.

CONCATENATE TMP_INT '.' TMP_SCALE INTO TMP_VALUE .
CONDENSE TMP_VALUE.

PO_VALUE = TMP_VALUE.
IF PO_VALUE = '' OR PO_VALUE IS INITIAL.
PO_VALUE = 0.
ENDIF.
ENDCASE.

ENDFUNCTION.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9437124/viewspace-1021397/,如需轉載,請註明出處,否則將追究法律責任。

相關文章