ABAP--将数字金额转换为人民币大写字符串(增加UNICODE版本)

1、No Uncode Version

FUNCTION Z_CONVERT_NUMERIC_TO_CHINESE.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     VALUE(PI_MONEY) LIKE  BSEG-DMBTR
*"  EXPORTING
*"     REFERENCE(PO_CHINESE)
*"  EXCEPTIONS
*"      WRONG_MONEY
*"----------------------------------------------------------------------
  DATA:MONEY_STR(16).
  DATA:I TYPE I,
       J TYPE I,
       UNITS_OFF TYPE I,
       CURNT_OFF TYPE I.
  DATA:LASTD TYPE N,CURNTD TYPE N.
  DATA:CWORD(2),CUNIT(2),OCUNIT(2).
  IF PI_MONEY = 0.
    PO_CHINESE = '零'.
    EXIT.
  ENDIF.
  MONEY_STR = PI_MONEY.
  IF MONEY_STR CN '0123456789. '.
    RAISE WRONG_MONEY.
  ENDIF.
*人为删除小数点
  IF MONEY_STR CS '.'.
    I = SY-FDPOS + 1.
    MONEY_STR+SY-FDPOS = MONEY_STR+I.
  ENDIF.
  CONDENSE MONEY_STR NO-GAPS.
  DATA:UNITS(30) VALUE '分角元拾佰仟万拾佰仟亿拾佰仟万',
       DIGTS(20) VALUE '零壹贰叁肆伍陆柒捌玖'.
  LASTD = 0.
  CURNT_OFF = STRLEN( MONEY_STR ) - 1.
  CLEAR OCUNIT.
  WHILE CURNT_OFF >= 0.
    CURNTD = MONEY_STR+CURNT_OFF(1). "提取数字
    I = CURNTD * 2.
    CWORD = DIGTS+I(2).              "取数字对应的汉字
    CUNIT = UNITS+UNITS_OFF(2).      "取单位
    I = UNITS_OFF / 2.               "取单位对应的位置
    IF CURNTD = 0.                   "Current digit is 0
      IF I = 2.
        CLEAR:CWORD.
        IF CURNT_OFF = 0.            "最后一个数字为0清除单位
          CLEAR:CUNIT.
        ENDIF.
      ELSEIF I = 6 OR I = 10.
        OCUNIT = CUNIT.
        CLEAR:CWORD.
        CLEAR:CUNIT.
        IF CURNT_OFF = 0.            "最后一个数字为0清除单位
        ENDIF.
      ELSEIF LASTD = 0.              "前面的一个数字是零则避免再次填写零
        CLEAR:CWORD,CUNIT.
      ELSE.
        CLEAR:CUNIT.
      ENDIF.
    elseif I = 10.
      CLEAR OCUNIT.
    ENDIF.
    IF OCUNIT ne space and  CURNTD ne 0.
      CONCATENATE OCUNIT PO_CHINESE INTO PO_CHINESE.
      CLEAR OCUNIT.
    endif.
    CONCATENATE CWORD CUNIT PO_CHINESE INTO PO_CHINESE.
    LASTD = CURNTD.
    SUBTRACT 1 FROM CURNT_OFF.
    ADD 2 TO UNITS_OFF.
  ENDWHILE.
  IF PO_CHINESE NS '分'.
    CONCATENATE PO_CHINESE '整' INTO PO_CHINESE.
  ELSE.
    CWORD = PO_CHINESE.
    IF CWORD = '零'.
      SHIFT PO_CHINESE BY 2 PLACES.
    ENDIF.
  ENDIF.
ENDFUNCTION.

2 Unicode Version

FUNCTION zfuc_numeric_to_chinese.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(IV_MONEY) TYPE  BF_DMBTR
*"  EXPORTING
*"     REFERENCE(EV_MONEY)
*"  EXCEPTIONS
*"      WRONG_MONEY
*"----------------------------------------------------------------------
  IF iv_money = 0.
    ev_money = '零'.
    EXIT.
  ENDIF.
  DATA:money_str(33).
  money_str = iv_money.
   CONDENSE money_str NO-GAPS.
  IF money_str CN '0123456789. '.
    RAISE wrong_money.
  ENDIF.
  DATA:i TYPE i.
  IF money_str CS '.'.
    i = sy-fdpos + 1.
    money_str+sy-fdpos = money_str+i.
  ENDIF.
  CONDENSE money_str NO-GAPS.
  DATA:units_off TYPE i,
       curnt_off TYPE i.
  DATA:lastd TYPE n,curntd TYPE n.
  DATA:cword(2),weight(2).
  DATA:units(30) VALUE '分角元拾佰仟万拾佰仟亿拾佰仟万',
       digts(20) VALUE '零壹贰叁肆伍陆柒捌玖'.

* clear:ev_money,units_off.
  lastd = 0.
  curnt_off = STRLEN( money_str ) - 1.
  WHILE curnt_off >= 0.
    curntd = money_str+curnt_off(1).
    i = curntd.
    cword = digts+i(1).

    weight = units+units_off(1).

    i = units_off / 1.
    IF curntd = 0.             "Current digit is 0
      IF i = 2 OR i = 6 OR i = 10.
        CLEAR:cword.
        IF curnt_off = 0.
          CLEAR:weight.
        ENDIF.
      ELSEIF lastd = 0.
        CLEAR:cword,weight.
      ELSE.
        CLEAR:weight.
      ENDIF.
    ENDIF.
    CONCATENATE cword weight ev_money INTO ev_money.
    lastd = curntd.
    SUBTRACT 1 FROM curnt_off.
    ADD 1 TO units_off.
  ENDWHILE.
  IF ev_money NS '分'.
    CONCATENATE ev_money '整' INTO ev_money.
  ELSE.
    cword = ev_money.
    IF cword = '零'.
      SHIFT ev_money BY 1 PLACES.
    ENDIF.
  ENDIF.

ENDFUNCTION


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/CompassButton/archive/2006/09/05/1180463.aspx

你可能感兴趣的:(SAP,reference,interface)