ABAP – Treating different number formats

SAPYou might face a problem when users have number format set differently in their user profiles (TCode SU01, table USR01). By default there are 3 formats available and this can cause troubles when processing user entered data and converting it into internal SAP DB format. Therefore you can use the following piece of code to check which format is applied on the user’s PC and format the numbers accordingly.

DATA:
  l_dcpfm    TYPE usr01-dcpfm,
  l_db_value TYPE char30,
  l_number   TYPE dec5_2 .

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

CASE l_dcpfm.
  WHEN ''.               "Format: 1.234.567,89
    REPLACE ALL OCCURRENCES OF '.' IN l_db_value WITH ''.
    TRANSLATE l_db_value USING ',.'.
  WHEN 'X'.              "Format: 1,234,567.89
    REPLACE ALL OCCURRENCES OF ',' IN l_db_value WITH ''.
  WHEN 'Y'.              "Format: 1 234 567,89
    TRANSLATE l_db_value USING ',.'.
ENDCASE.

TRY.
    l_number = l_db_value.
  CATCH cx_sy_conversion_no_number.
ENDTRY.

One thought on “ABAP – Treating different number formats

Leave a Reply