ABAP Function
ABAP Coding过程中比较好用的Function整理。
1.获取Domain信息
代码实例:
代码语言:javascript复制"********************************获取domain
FORM f_get_domain.
DATA:fs_taba TYPE dd07v.
DATA:it_taba TYPE STANDARD TABLE OF dd07v.
DATA:it_tabb TYPE STANDARD TABLE OF dd07v.
CALL FUNCTION 'DD_DOMA_GET'
EXPORTING
DOMAIN_NAME = 'XXX'
LANGU = SY-LANGU
WITHTEXT = 'X'
TABLES
DD07V_TAB_A = it_taba
DD07V_TAB_N = it_tabb
EXCEPTIONS
ILLEGAL_VALUE = 1
OP_FAILURE = 2
OTHERS = 3.
IF SY-SUBRC = 0.
LOOP AT it_taba INTO fs_taba.
WRITE:/ sy-tabix,fs_taba-domvalue_l,fs_taba-ddtext.
ENDLOOP.
LOOP AT it_tabb INTO fs_taba.
WRITE:/ sy-tabix,fs_taba-domvalue_l,fs_taba-ddtext.
ENDLOOP.
ENDIF.
ENDFORM.
2.非金额单位数据转换
代码实例:
代码语言:javascript复制"*************************单位数据转换
FORM f_change_unit.
DATA:v_out TYPE P DECIMALS 1.
DATA:v_in TYPE P DECIMALS 1 VALUE 1000.
CALL FUNCTION 'UNIT_CONVERSION_SIMPLE'
EXPORTING
INPUT = v_in
* NO_TYPE_CHECK = 'X'
* ROUND_SIGN = ' '
UNIT_IN = 'G'
UNIT_OUT = 'KG'
IMPORTING
OUTPUT = v_in.
IF SY-SUBRC <> 0.
MESSAGE s000 WITH 'error'.
ELSE.
MESSAGE s000 WITH v_in.
* Implement suitable error handling here
ENDIF.
ENDFORM.
3.获取金额转换汇率
代码实例:
代码语言:javascript复制"获取汇率转换汇率
FORM f_get_rate.
DATA:lv_type TYPE tcurr-kurst.
DATA:lv_rate TYPE P LENGTH 15 DECIMALS 2.
DATA:lv_msg TYPE string.
lv_type = 'AVTW'.
"获取汇率
CALL FUNCTION 'READ_EXCHANGE_RATE'
EXPORTING
CLIENT = sy-mandt
DATE = sy-datum
FOREIGN_CURRENCY = 'RMB'
LOCAL_CURRENCY = 'USD'
TYPE_OF_RATE = lv_type
IMPORTING
EXCHANGE_RATE = lv_rate
EXCEPTIONS
NO_RATE_FOUND = 1
NO_FACTORS_FOUND = 2
NO_SPREAD_FOUND = 3
DERIVED_2_TIMES = 4
OVERFLOW = 5
ZERO_RATE = 6.
IF sy-subrc <> 0.
lv_msg = 'Get exchange rate failed!'.
MESSAGE lv_msg TYPE 'E'.
ELSE.
lv_msg = 'Translate RMB TO USD Rate:' && lv_rate.
MESSAGE lv_msg TYPE 'S'.
ENDIF.
ENDFORM.
4.字符串转换到字符串内表
Function Group:SCMS_CONV
字符串转换到字符串内表:SCMS_STRING_TO_FTEXT
字符串转换成Xstring: SCMS_STRING_TO_XSTRING
字符串内表转换Xstring: SCMS_FTEXT_TO_XSTRING
字符串内表转换String: SCMS_FTEXT_TO_STRING
代码实例:
代码语言:javascript复制"***********************字符串转换为字符串内表
FORM f_trans_strtoitab.
TYPES:BEGIN OF t_str,
str(255) TYPE C,
END OF t_str.
DATA:lt_str TYPE TABLE OF t_str.
DATA:ls_str LIKE LINE OF lt_str.
DATA:lv_length TYPE i.
CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
EXPORTING
TEXT = 'hello world懒得看缴费对的看法角度abcdef'
IMPORTING
LENGTH = lv_length
TABLES
FTEXT_TAB = lt_str.
WRITE:/ lv_length.
LOOP AT lt_str INTO ls_str.
WRITE:/ ls_str-str.
ENDLOOP.
ENDFORM.
5.根据字符串公式计算值
代码实例:
代码语言:javascript复制"******************通过公式计算分数
TYPES:t_p TYPE p LENGTH 15 DECIMALS 2.
FORM f_calculate_formula
USING iv_formula TYPE string
CHANGING ev_result TYPE t_p.
DATA:lv_qsollwerte TYPE qsollwerte.
DATA:lv_float TYPE float.
CALL FUNCTION 'EVAL_FORMULA'
EXPORTING
formula = iv_formula
IMPORTING
value = lv_float.
ev_result = lv_float.
ENDFORM.
FORM f_test_cal.
DATA:lv_formula TYPE string.
DATA:lv_res TYPE P LENGTH 15 DECIMALS 2.
lv_formula = '1000/200 20*(0.25/1.2)*10'.
PERFORM f_calculate_formula USING lv_formula CHANGING lv_res.
lv_res = lv_res.
WRITE:/ lv_res.
ENDFORM.
6.字符串前导0的添加与删除
代码实例:
代码语言:javascript复制"******************字符串前补0,删除前导0
FORM f_numb_0.
"*******************************************数字符串前补0
WRITE:/'数字字符串前补0'.
DATA: input_p(3) type N value '344'.
DATA: output_p(20) type N.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = input_p
IMPORTING
OUTPUT = output_p
.
WRITE:/ input_p,'----', output_p.
"*****************************************消除数字符串前0
DATA: input_p1(10) type N value '00034'.
DATA: output_p1(3) type N .
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
INPUT = input_p1
IMPORTING
OUTPUT = output_p1
.
WRITE:/ input_p1,'----',output_p1.
ENDFORM.
7.全角半角转换
代码实例:
代码语言:javascript复制"*******************************全角半角转换
FORM f_change_quanjiao.
DATA:lv_c TYPE C LENGTH 20.
lv_c = ' hello自大'.
"全角转换半角
CALL FUNCTION 'SJIS_DBC_TO_SBC'
EXPORTING
ALL = 'X'
CHANGING
TEXT = lv_c.
IF SY-SUBRC <> 0.
ELSE.
WRITE:/ lv_c.
CONDENSE lv_c NO-GAPS.
WRITE:/ lv_c.
ENDIF.
"半角转换全角
CALL FUNCTION 'SJIS_SBC_TO_DBC'
EXPORTING
ALL = 'X'
* IMPORTING
* CONVERTED =
* HIT =
CHANGING
TEXT = lv_c
EXCEPTIONS
OVERFLOW = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE 'error' TYPE 'E'.
ELSE.
WRITE:/ lv_c.
ENDIF.
ENDFORM.