代码参见
DATA lv_curr TYPE bapicurr-bapicurr.
*统一转换给外部数据,保留精度
CALL FUNCTION 'BAPI_CURRENCY_CONV_TO_EXTERNAL'
EXPORTING
currency = ''
amount_internal = lv_price
IMPORTING
amount_external = lv_curr.
CLEAR lv_currc.
lv_currc = lv_curr.
SPLIT lv_currc AT '.' INTO DATA(lv_tmp1) DATA(lv_tmp2).
CONDENSE:lv_tmp1,lv_tmp2.
DATA(lv_len) = strlen( lv_tmp1 ).
DATA(lv_len1) = lv_len MOD 3.
DATA(lv_len2) = lv_len DIV 3.
*根据整除保留的整数进行循环,无偏移时最后一次不用循环默认为0
DO lv_len2 TIMES.
*小于1和无偏移时,最后一次的逗号不用加
IF lv_len = 3 OR ( lv_len1 = 0 AND sy-index = lv_len2 ).
EXIT.
ENDIF.
*偏移按照第一次进行偏移
IF lv_len1 <> 0.
DATA(lv_len3) = lv_len1 + sy-index - 4 + 3 * sy-index .
ENDIF.
*无偏移3的倍数+当前次数-1,1为第一次偏移
IF lv_len1 = 0.
lv_len3 = 3 * sy-index + sy-index - 1.
ENDIF.
*从中间插入
DATA(lv_len4) = strlen( lv_tmp1 ) - lv_len3.
CONCATENATE lv_tmp1+0(lv_len3) ',' lv_tmp1+lv_len3(lv_len4) INTO lv_tmp1.
ENDDO.
*保留2位小数
lv_currc = lv_tmp1 && '.' && lv_tmp2+0(2).
CONDENSE lv_currc.