技术开发 频道

ABAP实例:将数字金额转换为人民币大写字符串

【IT168 技术文章】

  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
    *"----------------------------------------------------------------------
      if pi_money = 0.
        po_chinese
= ''.
        exit.
      endif.
      data:money_str(
13).
      money_str
= pi_money.
      
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:po_chinese,units_off.
      lastd
= 0.
      curnt_off
= strlen( money_str ) - 1.
      
while curnt_off >= 0.
        curntd
= money_str+curnt_off(1).
        i
= curntd * 2.
        cword
= digts+i(2).

        weight
= units+units_off(2).


        i
= units_off / 2.
        
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 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.
0
相关文章