描述:
ALV单元格在可输入状态下,金额和数量类型的字段小数点的位置会发生错误,数量是QUAN类型,金额是CURR类型。
透明表:
错误情况如下:
数量输入66,结果显示0.066,小数点提前3位,差1000倍。
金额输入7,结果显示0.07,小数点提前两位,差100倍。
错误源码
REPORT ZDCH_09_TEST2.
TYPES:BEGIN OF GTY_EKKO,
EBELN TYPE EKKO-EBELN,
MENGE TYPE EKPO-MENGE,
NETWR TYPE EKPO-NETWR,
END OF GTY_EKKO.
DATA:GIT_EKKO TYPE STANDARD TABLE OF GTY_EKKO,
GWA_EKKO TYPE GTY_EKKO.
DATA:GIT_FCAT TYPE LVC_T_FCAT,
GWA_FCAT LIKE LINE OF GIT_FCAT,
GWA_LAYOUT TYPE LVC_S_LAYO,
GWA_GLAY TYPE LVC_S_GLAY.
INITIALIZATION.
PERFORM FRM_SET_LAYOUT.
PERFORM FRM_SET_CATALOG.
START-OF-SELECTION.
PERFORM FRM_GET_DATA.
END-OF-SELECTION.
PERFORM FRM_DISPLAY_DATA.
*&---------------------------------------------------------------------*
*& Form FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_SET_LAYOUT .
GWA_LAYOUT-ZEBRA = 'X'.
GWA_LAYOUT-SEL_MODE = 'A'.
GWA_LAYOUT-CWIDTH_OPT = 'X'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_CATALOG
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_SET_CATALOG .
GWA_FCAT-COLTEXT = 'EBELN'.
GWA_FCAT-FIELDNAME = 'EBELN'.
GWA_FCAT-COL_POS = 1.
GWA_FCAT-OUTPUTLEN = '10'.
APPEND GWA_FCAT TO GIT_FCAT.
GWA_FCAT-COLTEXT = '数量'.
GWA_FCAT-FIELDNAME = 'MENGE'.
GWA_FCAT-EDIT = 'X'.
GWA_FCAT-COL_POS = 2.
GWA_FCAT-OUTPUTLEN = '10'.
APPEND GWA_FCAT TO GIT_FCAT.
GWA_FCAT-COLTEXT = '金额'.
GWA_FCAT-FIELDNAME = 'NETWR'.
GWA_FCAT-EDIT = 'X'.
GWA_FCAT-COL_POS = 3.
GWA_FCAT-OUTPUTLEN = '10'.
APPEND GWA_FCAT TO GIT_FCAT.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_GET_DATA .
SELECT EBELN
MENGE
NETWR
INTO CORRESPONDING FIELDS OF TABLE GIT_EKKO
FROM EKPO
UP TO 10 ROWS.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_DISPLAY_DATA .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
IS_LAYOUT_LVC = GWA_LAYOUT
IT_FIELDCAT_LVC = GIT_FCAT
TABLES
T_OUTTAB = GIT_EKKO[]
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC = 0.
ENDIF.
ENDFORM.
解决方法:
1、对于货币字段,catalog中再添加一个“指定数据类型”的属性datatype,如下:
wa_fieldcat-datatype = 'CURR' . " 指定数据类型
2、对于数量字段,catalog中也要添加一个“指定数据类型”的属性datatype,如下:
wa_fieldcat-datatype = 'QUAN' . " 指定数据类型
wa_fieldcat-inttype = 'C' . "这个是指定字段的类型为C
正确源码:
REPORT ZDCH_09_TEST2.
TYPES:BEGIN OF GTY_EKKO,
EBELN TYPE EKKO-EBELN,
MENGE TYPE EKPO-MENGE,
NETWR TYPE EKPO-NETWR,
END OF GTY_EKKO.
DATA:GIT_EKKO TYPE STANDARD TABLE OF GTY_EKKO,
GWA_EKKO TYPE GTY_EKKO.
DATA:GIT_FCAT TYPE LVC_T_FCAT,
GWA_FCAT LIKE LINE OF GIT_FCAT,
GWA_LAYOUT TYPE LVC_S_LAYO,
GWA_GLAY TYPE LVC_S_GLAY.
INITIALIZATION.
PERFORM FRM_SET_LAYOUT.
PERFORM FRM_SET_CATALOG.
START-OF-SELECTION.
PERFORM FRM_GET_DATA.
END-OF-SELECTION.
PERFORM FRM_DISPLAY_DATA.
*&---------------------------------------------------------------------*
*& Form FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_SET_LAYOUT .
GWA_LAYOUT-ZEBRA = 'X'.
GWA_LAYOUT-SEL_MODE = 'A'.
GWA_LAYOUT-CWIDTH_OPT = 'X'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_CATALOG
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_SET_CATALOG .
GWA_FCAT-COLTEXT = 'EBELN'.
GWA_FCAT-FIELDNAME = 'EBELN'.
GWA_FCAT-COL_POS = 1.
GWA_FCAT-OUTPUTLEN = '10'.
APPEND GWA_FCAT TO GIT_FCAT.
GWA_FCAT-COLTEXT = '数量'.
GWA_FCAT-FIELDNAME = 'MENGE'.
GWA_FCAT-DATATYPE = 'QUAN' . "指定该字段为数量
GWA_FCAT-INTTYPE = 'C' . "指定该字段类型为C
GWA_FCAT-EDIT = 'X'.
GWA_FCAT-COL_POS = 2.
GWA_FCAT-OUTPUTLEN = '10'.
APPEND GWA_FCAT TO GIT_FCAT.
GWA_FCAT-COLTEXT = '金额'.
GWA_FCAT-FIELDNAME = 'NETWR'.
GWA_FCAT-DATATYPE = 'CURR' . "指定该字段为金额
GWA_FCAT-EDIT = 'X'.
GWA_FCAT-COL_POS = 3.
GWA_FCAT-OUTPUTLEN = '10'.
APPEND GWA_FCAT TO GIT_FCAT.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_GET_DATA .
SELECT EBELN
MENGE
NETWR
INTO CORRESPONDING FIELDS OF TABLE GIT_EKKO
FROM EKPO
UP TO 10 ROWS.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_DISPLAY_DATA .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
IS_LAYOUT_LVC = GWA_LAYOUT
IT_FIELDCAT_LVC = GIT_FCAT
TABLES
T_OUTTAB = GIT_EKKO[]
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC = 0.
ENDIF.
ENDFORM.