Pages

Tuesday, June 7, 2016

ABAP Code Snippet

1)  Convert String/char  into Amount/quantity field based on Decimal notation set in the user profile. 





  DATA:
    l_thousand_sep    TYPE char1,
    l_dcpfm           TYPE xudcpfm,
    lv_number_as_char TYPE char21.

*--------------------------------------------------------------------*

  lv_number_as_char = '100,000.00'.

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

  ASSERT sy-subrc 0.
  IF l_dcpfm EQ 'X'.
    l_thousand_sep ','.
  ELSEIF l_dcpfm EQ ' '.
    l_thousand_sep '.'.
* in case of  'Y' the thousand separator is SPACE, so no need to replace
  ENDIF.

  IF l_thousand_sep IS NOT INITIAL.
    REPLACE ALL OCCURRENCES OF l_thousand_sep IN lv_number_as_char WITH ''.
  ENDIF.
  REPLACE ALL OCCURRENCES OF ',' IN lv_number_as_char WITH '.'.

  CONDENSE lv_number_as_char NO-GAPS.

  TRY.
      ev_number_as_dec lv_number_as_char.
    CATCH cx_sy_conversion_no_number.

  ENDTRY.

No comments:

Post a Comment