ITPub博客

首页 > IT职业 > IT生活 > 自己写的截取小数位的函数

自己写的截取小数位的函数

原创 IT生活 作者:tolyhuang 时间:2009-04-28 15:23:34 0 删除 编辑
用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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-07-22

  • 博文量
    268
  • 访问量
    1411193