【IT168 技术文章】
ABAP中金额转大写的方法:
*&---------------------------------------------------------------------*
*& Report Z_BARRY_CONV_AMOUNT *
*& *
*&---------------------------------------------------------------------*
*& 金额大写转换 *
*&---------------------------------------------------------------------*
REPORT Z_BARRY_CONV_AMOUNT .
TYPES: CUR6 TYPE P DECIMALS 6 ,
CUR2 TYPE P DECIMALS 2 .
DATA VA1 TYPE CUR2.
DATA STR(40) TYPE C .
VA1 = '232.23'.
PERFORM CONV_AMOUNT USING VA1
CHANGING STR.
WRITE STR.
*&--------------------------------------------------------------------*
*& Form conv_amount
*&--------------------------------------------------------------------*
FORM CONV_AMOUNT USING VALUE(F_SOURCE)
CHANGING VALUE(F_RESULT).
DATA: SCR(30) TYPE C, RES(60) TYPE C,FEN(2) TYPE C .
DATA: LEN TYPE I, C1 TYPE I, C2 TYPE I, C3 TYPE I, C4 TYPE I.
DATA: D1(1) TYPE C, D2(1) TYPE C, D3 TYPE I.
DATA: DIGIT(2) TYPE C, WEIGHT(2) TYPE C.
DATA: RULE1(20) TYPE C VALUE '零壹贰叁肆伍陆柒捌玖'.
DATA: RULE2(30) TYPE C VALUE '分角元拾佰仟万拾佰仟亿拾佰仟万'.
SCR = F_SOURCE * 100.
CONDENSE SCR NO-GAPS.
IF SCR = '0'.
RES = '零元'.
ELSE.
LEN = STRLEN( SCR ).
C1 = 0.
D1 = '0'.
CLEAR RES.
DO LEN TIMES.
C1 = C1 + 1.
C2 = LEN - C1.
D2 = SCR+C2(1) .
IF D2 = '0'.
D3 = 0.
ELSE.
D3 = D2 * 2.
ENDIF.
DIGIT = RULE1+D3(2) .
C3 = ( C1 - 1 ) * 2.
WEIGHT = RULE2+C3(2) .
IF D2 = '0'.
IF C1 = 3.
DIGIT = ''.
ELSEIF C1 = 7.
DIGIT = ''.
IF LEN > 10 .
C4 = LEN - 10.
IF SCR+C4(4) = '0000'.
WEIGHT = ''.
ENDIF.
ENDIF.
ELSEIF C1 = 11.
DIGIT = ''.
ELSEIF D1 = '0'.
DIGIT = ''.
WEIGHT = ''.
ELSE.
WEIGHT = ''.
ENDIF.
ENDIF.
* CONCATENATE digit weight res INTO res. "汉字间是否加进空格
CONCATENATE DIGIT WEIGHT RES INTO RES SEPARATED BY SPACE.
D1 = D2.
ENDDO.
ENDIF.
LEN = STRLEN( RES ) - 2.
FEN = RES+LEN(2).
IF FEN <> '分' .
CONCATENATE RES ' 整' INTO F_RESULT.
ELSE.
F_RESULT = RES.
ENDIF.
ENDFORM. "conv_amount
*------------------------------------------------------------*
FOR UNICODE 系统:
REPORT z_barry_test NO STANDARD PAGE HEADING .
TYPES: CUR6 TYPE P DECIMALS 6 ,
CUR2 TYPE P DECIMALS 2 .
DATA VA1 TYPE CUR2.
DATA STR(40) TYPE C .
VA1 = '1000123.5'.
PERFORM CONV_AMOUNT USING VA1
CHANGING STR.
WRITE STR.
*&--------------------------------------------------------------------*
*& Form conv_amount
*&--------------------------------------------------------------------*
FORM CONV_AMOUNT USING VALUE(F_SOURCE)
CHANGING VALUE(F_RESULT).
DATA: SCR(30) TYPE C, RES(60) TYPE C,FEN(2) TYPE C .
DATA: LEN TYPE I, C1 TYPE I, C2 TYPE I, C3 TYPE I, C4 TYPE I.
DATA: D1(1) TYPE C, D2(1) TYPE C, D3 TYPE I.
DATA: DIGIT(2) TYPE C, WEIGHT(2) TYPE C.
DATA: RULE1(20) TYPE C VALUE '零壹贰叁肆伍陆柒捌玖'.
DATA: RULE2(30) TYPE C VALUE '分角元拾佰仟万拾佰仟亿拾佰仟万'.
SCR = F_SOURCE * 100.
CONDENSE SCR NO-GAPS.
IF SCR = '0'.
RES = '零元'.
ELSE.
LEN = STRLEN( SCR ).
C1 = 0.
D1 = '0'.
CLEAR RES.
DO LEN TIMES.
C1 = C1 + 1.
C2 = LEN - C1.
D2 = SCR+C2(1) .
IF D2 = '0'.
D3 = 0.
ELSE.
D3 = D2.
ENDIF.
DIGIT = RULE1+D3(1) .
C3 = ( C1 - 1 ) .
WEIGHT = RULE2+C3(1) .
IF D2 = '0'.
IF C1 = 3.
DIGIT = ''.
ELSEIF C1 = 7.
DIGIT = ''.
IF LEN > 10 .
C4 = LEN - 10.
IF SCR+C4(4) = '0000'.
WEIGHT = ''.
ENDIF.
ENDIF.
ELSEIF C1 = 11.
DIGIT = ''.
ELSEIF D1 = '0'.
DIGIT = ''.
WEIGHT = ''.
ELSE.
WEIGHT = ''.
ENDIF.
ENDIF.
CONCATENATE DIGIT WEIGHT RES INTO RES .
D1 = D2.
ENDDO.
ENDIF.
LEN = STRLEN( RES ) - 1.
FEN = RES+LEN(1).
IF FEN <> '分' .
CONCATENATE RES '整' INTO F_RESULT.
ELSE.
F_RESULT = RES.
ENDIF.
ENDFORM. "conv_amount