Wednesday, June 6, 2018

Translate amount format

REPORT zqc_num_format.
DATA lv_text  TYPE string.
lv_text = '987,123,456.12'.

PERFORM f_translate_amount CHANGING lv_text.

WRITE / lv_text.

FORM f_translate_amount  CHANGING ch_text TYPE string.


  DATA:    w_compos TYPE i,
           w_comlen TYPE i,
           w_dotpos TYPE i.

  CONSTANTS:   c_dot_sym TYPE char01 VALUE '.',
               c_space   TYPE char01 VALUE ' ',
               c_com_sym TYPE char01 VALUE ','.

  CONDENSE ch_text NO-GAPS.

* Find first occurrence of dot character on amount field
  FIND FIRST OCCURRENCE OF c_dot_sym IN ch_text MATCH OFFSET w_dotpos.

* Find first occurence of comma character on amount field
  FIND FIRST OCCURRENCE OF c_com_sym IN ch_text MATCH OFFSET w_compos MATCH LENGTH w_comlen.

* If dot comes before the comma, remove all occurrences of dots and replace the comma by dot.
  IF w_dotpos LT w_compos.

    REPLACE ALL OCCURRENCES OF c_dot_sym IN ch_text WITH c_space.

    REPLACE ALL OCCURRENCES OF c_com_sym IN ch_text WITH c_dot_sym.

* If comma comes before the dot, replace the comma by dot.
  ELSE.

    REPLACE ALL OCCURRENCES OF c_com_sym IN ch_text WITH c_space.

  ENDIF.

* Remove blank spaces
  CONDENSE ch_text NO-GAPS.

  CLEAR: w_dotpos,
         w_compos,
         w_comlen.


ENDFORM. " F_TRANSLATE_AMOUNT

No comments:

Post a Comment