ABAP中OO的方式实现金额大写转化,见如下代码
class zcl_abap_util_temp definition
public
final
create public .
public section.
constants c_1 type c value '壹' ##NO_TEXT.
constants c_2 type c value '贰' ##NO_TEXT.
constants c_3 type c value '叁' ##NO_TEXT.
constants c_4 type c value '肆' ##NO_TEXT.
constants c_5 type c value '伍' ##NO_TEXT.
constants c_6 type c value '陆' ##NO_TEXT.
constants c_7 type c value '柒' ##NO_TEXT.
constants c_8 type c value '捌' ##NO_TEXT.
constants c_9 type c value '玖' ##NO_TEXT.
constants c_00(2) type c value '零零' ##NO_TEXT.
constants c_0 type c value '零' ##NO_TEXT.
constants c_shi type c value '拾' ##NO_TEXT.
constants c_bai type c value '佰' ##NO_TEXT.
constants c_qian type c value '仟' ##NO_TEXT.
constants c_wan type c value '万' ##NO_TEXT.
constants c_yi type c value '亿' ##NO_TEXT.
constants c_yuan type c value '元' ##NO_TEXT.
constants c_jiao type c value '角' ##NO_TEXT.
constants c_fen type c value '分' ##NO_TEXT.
constants c_zheng type c value '整' ##NO_TEXT.
class-methods action_cap_conversion
importing
!iv_amount type dmbtr
returning
value(rv_capitalization) type string .
protected section.
private section.
ENDCLASS.
CLASS ZCL_ABAP_UTIL_TEMP IMPLEMENTATION.
*
---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_ABAP_UTIL_TEMP=>ACTION_CAP_CONVERSION
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_AMOUNT TYPE DMBTR
* | [<-()] RV_CAPITALIZATION TYPE STRING
* +--------------------------------------------------------------------------------------
method action_cap_conversion.
************************************************************************
*人民币金额转化为大写
************************************************************************
data:begin of ls_djsz,"等级数值
yjsz type i, "亿级别数值
wjsz type i, "万级别数值
gjsz type i, "元级别数值
fjsz type i, "分级别数值
end of ls_djsz.
data:begin of ls_djdx,"等级大写
yjdx type string, "亿级别大写
wjdx type string, "万级别大写
gjdx type string, "元级别大写
fjdx type string, "分级别大写
end of ls_djdx.
data:lv_amount type dmbtr,
lv_index type i,
lv_p type p,
lv_num4(4) type c,
lv_num2(2) type c,
lv_c(1) type c.
define hb00.
*合并相邻的0
do .
replace all occurrences of c_00
in &1 with c_0 in character mode.
if sy-subrc ne 0.
exit.
endif.
enddo.
end-of-definition.
define get_dzdx.
*获取单个数值大写
case &1.
when 0.
&2 = c_0.
when 1.
&2 = c_1.
when 2.
&2 = c_2.
when 3.
&2 = c_3.
when 4.
&2 = c_4.
when 5.
&2 = c_5.
when 6.
&2 = c_6.
when 7.
&2 = c_7.
when 8.
&2 = c_8.
when 9.
&2 = c_9.
when OTHERS.
ENDCASE.
end-of-definition.
define get_djdx.
check &1 ne 0."不为0
clear:lv_num4.
*获取等级大写
lv_num4 = &1.
*补签到0
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = lv_num4
importing
output = lv_num4.
clear lv_index.
do 4 times.
lv_Index = sy-index - 1.
clear:lv_c.
get_dzdx lv_num4+lv_index(1) lv_c.
&2 = &2 && lv_c.
if lv_c eq c_0."零不带单位
else.
case lv_index.
when 0. "千位
&2 = &2 && c_qian.
when 1."百位
&2 = &2 && c_bai.
when 2."十位
&2 = &2 && c_shi.
when 3.
endcase.
endif.
enddo.
*相邻的零合并
hb00 &2.
*删除末尾的零
SHIFT &2 RIGHT DELETING TRAILING c_0.
CONDENSE &2.
end-of-definition.
lv_amount = iv_amount.
********************数额分级 START***********************************
lv_p = lv_amount / 100000000.
ls_djsz-yjsz = floor( lv_p ).
lv_amount = lv_amount - ls_djsz-yjsz * 100000000.
lv_p = lv_amount / 10000.
ls_djsz-wjsz = floor( lv_p ).
lv_amount = lv_amount - ls_djsz-wjsz * 10000.
ls_djsz-gjsz = floor( lv_amount ).
lv_amount = lv_amount - ls_djsz-gjsz .
ls_djsz-fjsz = lv_amount * 100.
********************数额分级 END***********************************
********************数值转大写 START***********************************
if ls_djsz-yjsz ne 0.
get_djdx ls_djsz-yjsz ls_djdx-yjdx."亿级
endif.
if ls_djsz-wjsz ne 0.
get_djdx ls_djsz-wjsz ls_djdx-wjdx."万级
endif.
if ls_djsz-gjsz ne 0.
get_djdx ls_djsz-gjsz ls_djdx-gjdx."元级
endif.
if ls_djsz-fjsz ne 0."分级
clear:lv_num2.
*获取等级大写
lv_num2 = ls_djsz-fjsz.
*补签到0
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = lv_num2
importing
output = lv_num2.
if lv_num2+0(1) eq '0'.
ls_djdx-fjdx = ls_djdx-fjdx && c_0.
else.
clear lv_c.
get_dzdx lv_num2+0(1) lv_c.
ls_djdx-fjdx = ls_djdx-fjdx && lv_c && c_jiao.
endif.
if lv_num2+1(1) ne '0'.
clear lv_c.
get_dzdx lv_num2+1(1) lv_c.
ls_djdx-fjdx = ls_djdx-fjdx && lv_c && c_fen.
endif.
condense ls_djdx-fjdx.
endif.
********************数值转大写 END***********************************
********************RM大写拼接 START***********************************
if ls_djsz-yjsz ne 0.
rv_capitalization = rv_capitalization && ls_djdx-yjdx && c_yi."亿级
endif.
if ls_djsz-wjsz ne 0.
rv_capitalization = rv_capitalization && ls_djdx-wjdx && c_wan."万级
else.
rv_capitalization = rv_capitalization && c_0.
endif.
if ls_djsz-gjsz ne 0.
rv_capitalization = rv_capitalization && ls_djdx-gjdx."元级
else.
rv_capitalization = rv_capitalization && c_0.
endif.
if iv_amount >= 1."大于等于1元
rv_capitalization = rv_capitalization && c_yuan.
endif.
if ls_djsz-fjsz eq 0.
rv_capitalization = rv_capitalization && c_zheng.
else.
rv_capitalization = rv_capitalization && ls_djdx-fjdx.
endif.
*相邻的零合并
hb00 rv_capitalization.
*首位去零
shift rv_capitalization left deleting leading c_0.
********************RM大写拼接 END***********************************
endmethod.
ENDCLASS.