欢迎来到萧静默的博客

书山有路勤为径,学海无涯苦作舟。

BDC中日期数量金额格式转换

在BDC录屏中,数量和日期格式是和su3中的设置相关的,如果格式不一致BDC会报错,
例如:日期格式不一致报错

Tcode:SU3,可以看到小数点,日期格式以及时间格式的设置。

 

 

 

日期:格式 DD.MM.YYYY和中文环境不同,
小数:点好做千分位,逗号做小数点   所以需要转换

  • 1,日期&时间格式:方法1 write to ,这是个万金油,可以解决日期,数量,金额格式的问题;

DATA l_datum(10).
WRITE p_budat TO l_datum.


方法2,调用CONVERT_DATE_TO_EXTERNAL将日期转换为外部格式。

 

  • 2数量转换:注意不要忘了单位 UNIT

WRITE <lfs_alv>-romen TO gwa_stpo_r-romen UNIT <lfs_alv>-romei.

 

  • 3金额格式不一致报错:


方法1,  Write to

 DATA: lv_dmbtr TYPE BDCDATA-FVAL,

WRITE gt_bsik9-wrbtr_1 TO lv_dmbtr.
CONDENSE lv_dmbtr NO-GAPS.             

使用WRITE TO 后得到:

 

 WRITE TO  语法说明:

WRITE {source|(source_name)} TO destination
[LEFT-JUSTIFIED|CENTERED|RIGHT-JUSTIFIED]
    { { [EXPONENT exp]
        [NO-GROUPING]
        [NO-SIGN]
        [NO-ZERO]
        [CURRENCY cur]
        { {[DECIMALS dec][ROUND scale]}|[UNIT unit]}
}
      | {[ENVIRONMENT TIME FORMAT][TIME ZONE tz]} [STYLE stl]
}
    [USING{ {NO EDIT MASK}|{EDIT MASK mask} }]
    [ DD/MM/YY|MM/DD/YY|DD/MM/YYYY|MM/DD/YYYY|DDMMYY|MMDDYY|YYMMDD ].

Write具体成什么格式的日期,这与当前用户主数据所设定的格式来显示,用户主数据日期格式可以通过以下方式获取到:

  SELECT SINGLE dcpfm INTO l_dcpfm FROM usr01 WHERE bname = sy-uname.

 

 

 

 

 

 

复制代码
DATA: date_short TYPE c LENGTH 8,
      date_long  TYPE c LENGTH 10,
      date_mask  TYPE c LENGTH 8,
      date_mask2  TYPE c LENGTH 10.
WRITE sy-datum TO: date_short,
                   date_long,
                   date_mask  USING EDIT MASK 'DD/MM/YY',
                   date_mask2 USING EDIT MASK 'DD/MM/YYYY'.

WRITE: sy-datum,
     / date_short,
     / date_long,
     / date_mask ,
     / date_mask2."
复制代码

 

 

 

 


方法2,   使用函数转换,函数效果
正数测试

负数测试



输入

输出

 

 

 函数代码

FUNCTION z_bficonver_amount_format.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(I_STRING)
*"     REFERENCE(I_DECIMALS) TYPE  I
*"  EXPORTING
*"     REFERENCE(OUT_STRING)
*"----------------------------------------------------------------------
*======================================================================*
*                                                                      *
*  PROGRAM        : Z_BFICONVER_AMOUNT_FORMAT                          *
*  DESCRIPTION    : Convert the amount field format                        *
*                                                                      *
*                                                                      *
*======================================================================*
*                                                                      *
*  CREATION DATE  : 14/10/2020                                         *
*  AUTHOR         : Rainy.cai                                *
*                                                                      *
*======================================================================*
*                                                                      *
*  MODIFICATIONS LIST :                                                *
*                                                                      *
*  ----------|----------|----------|---------------------------------  *
*   N° MODIF |  AUTHOR  |   DATE   | MODIFICATION DESCRIPTION         *
*  ----------|----------|----------|---------------------------------  *
*  XXXDATE   |USER      |Date      |Description text of modification   *
*  ----------|----------|----------|---------------------------------  *
** 为清账程序转换金额字段格式
  DATA:l_dcpfm   LIKE usr01-dcpfm,
       l_string  TYPE char20,
       l_reverse TYPE char20,
       l_strout  TYPE char20,
       l_str1    TYPE string,
       l_str2    TYPE string,

       l_mask    TYPE string.
  DATA:l_lenth TYPE i.
  DATA:l_lenth2 TYPE i.
  CLEAR l_mask.
  l_str2 = '___'.
  CASE i_decimals.
    WHEN 2.
      l_str1 = '__'.
    WHEN 3.
      l_str1 = '___'.
    WHEN 4.
      l_str1 = '____'.
    WHEN OTHERS.
  ENDCASE.

**处理负数情况
  IF i_string < 0.
    l_string = abs( i_string ).
  ELSE.
    l_string = i_string.
  ENDIF.
  "remove Punctuation by space
  REPLACE '.' WITH '' INTO l_string.
  CONDENSE l_string NO-GAPS.
  l_lenth = strlen( l_string ).
  l_lenth2 = l_lenth - i_decimals.

  CASE l_lenth2.
    WHEN 1 OR 2 OR 3.
    WHEN 4 OR 5 OR 6.
    WHEN OTHERS.
  ENDCASE.
  "reverse string
  CALL FUNCTION 'STRING_REVERSE'
    EXPORTING
      string  = l_string
      lang    = sy-langu
    IMPORTING
      rstring = l_reverse.


  SELECT SINGLE dcpfm INTO l_dcpfm FROM usr01 WHERE bname = sy-uname.

  CASE l_dcpfm.
    WHEN ''.
      CASE l_lenth2.
        WHEN 1 OR 2 OR 3.
          CONCATENATE l_str1 l_str2 INTO l_mask SEPARATED BY ','.
        WHEN 4 OR 5 OR 6.
          CONCATENATE l_str2 l_str2 INTO l_mask SEPARATED BY '.'.
          CONCATENATE l_str1 l_mask INTO l_mask SEPARATED BY ','.
        WHEN 7 OR 8 OR 9.
          CONCATENATE l_str2 l_str2 l_str2 INTO l_mask SEPARATED BY '.'.
          CONCATENATE l_str1 l_mask INTO l_mask SEPARATED BY ','.
        WHEN 10 OR 11 OR 12.
          CONCATENATE l_str2 l_str2 l_str2 l_str2 INTO l_mask SEPARATED BY '.'.
          CONCATENATE l_str1 l_mask INTO l_mask SEPARATED BY ','.
        WHEN OTHERS.
      ENDCASE.
    WHEN 'X'.
      CASE l_lenth2.
        WHEN 1 OR 2 OR 3.
          CONCATENATE l_str1 l_str2 INTO l_mask SEPARATED BY '.'.
        WHEN 4 OR 5 OR 6.
          CONCATENATE l_str2 l_str2 INTO l_mask SEPARATED BY ','.
          CONCATENATE l_str1 l_mask INTO l_mask SEPARATED BY '.'.
        WHEN 7 OR 8 OR 9.
          CONCATENATE l_str2 l_str2 l_str2 INTO l_mask SEPARATED BY ','.
          CONCATENATE l_str1 l_mask INTO l_mask SEPARATED BY '.'.
        WHEN 10 OR 11 OR 12.
          CONCATENATE l_str2 l_str2 l_str2 l_str2 INTO l_mask SEPARATED BY ','.
          CONCATENATE l_str1 l_mask INTO l_mask SEPARATED BY '.'.
        WHEN OTHERS.
      ENDCASE.
    WHEN 'Y'.
      CASE l_lenth2.
        WHEN 1 OR 2 OR 3.
          CONCATENATE l_str1 l_str2 INTO l_mask SEPARATED BY ','.
        WHEN 4 OR 5 OR 6.
          CONCATENATE l_str2 l_str2 INTO l_mask SEPARATED BY space.
          CONCATENATE l_str1 l_mask INTO l_mask SEPARATED BY ','.
        WHEN 7 OR 8 OR 9.
          CONCATENATE l_str2 l_str2 l_str2 INTO l_mask SEPARATED BY space.
          CONCATENATE l_str1 l_mask INTO l_mask SEPARATED BY ','.
        WHEN 10 OR 11 OR 12.
          CONCATENATE l_str2 l_str2 l_str2 l_str2 INTO l_mask SEPARATED BY space.
          CONCATENATE l_str1 l_mask INTO l_mask SEPARATED BY ','.
        WHEN OTHERS.
      ENDCASE.
  ENDCASE.

  WRITE  l_reverse USING EDIT MASK l_mask TO l_strout.

  IF sy-subrc = 0.
    "reverse string
    CALL FUNCTION 'STRING_REVERSE'
      EXPORTING
        string  = l_strout
        lang    = sy-langu
      IMPORTING
        rstring = out_string.
  ENDIF.
  IF i_string < 0.
    CONCATENATE out_string '-' into out_string.
**    out_string = out_string * -1.
  ENDIF.

ENDFUNCTION.

转自:https://www.cnblogs.com/rainysblog/p/13819977.html

posted @ 2023-05-25 10:13  萧静默  阅读(244)  评论(0编辑  收藏  举报