Thursday, June 7, 2018

Remove blanks, comas and dots

REPORT zqc_num_format.
DATA v_ref1 TYPE string.
INCLUDE zqc_num_format_cls.

START-OF-SELECTION.
  DATA obj TYPE REF TO lcl_num_format.
  CREATE OBJECT obj.
  v_ref1 = obj->conv_format( v_ref = '123,456.12').
write / v_ref1.

  v_ref1 = obj->conv_format( v_ref = ' 1,123,456.12').
write / v_ref1.
call METHOD obj->nocomnodot
  EXPORTING v_ref =' 891,123,456.12'
  importing result = v_ref1.
WRITE / v_ref1.
-----------------------------------


CLASS lcl_num_format DEFINITION.
  PUBLIC SECTION.
    METHODS conv_format
      IMPORTING VALUE(v_ref) TYPE string
      RETURNING VALUE(result) TYPE string.

    METHODS nocomnodot
      IMPORTING VALUE(v_ref) TYPE string
      EXPORTING VALUE(result) TYPE string.

ENDCLASS.

CLASS lcl_num_format IMPLEMENTATION.
  METHOD conv_format.
      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 v_ref NO-GAPS.

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

* Find first occurence of comma character on amount field
      FIND FIRST OCCURRENCE OF c_com_sym IN v_ref 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 v_ref WITH c_space.

        REPLACE ALL OCCURRENCES OF c_com_sym IN v_ref WITH c_dot_sym.

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

        REPLACE ALL OCCURRENCES OF c_com_sym IN v_ref WITH c_space.

      ENDIF.

* Remove blank spaces
      CONDENSE v_ref NO-GAPS.
      result = v_ref.


      CLEAR:  w_dotpos,
              w_compos,
              w_comlen.

  ENDMETHOD.

    METHOD nocomnodot.
      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 v_ref NO-GAPS.

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

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

      REPLACE ALL OCCURRENCES OF c_dot_sym IN v_ref WITH c_space.

      REPLACE ALL OCCURRENCES OF c_com_sym IN v_ref WITH c_space.

* Remove blank spaces
      CONDENSE v_ref NO-GAPS.
      result = v_ref.


      CLEAR:  w_dotpos,
              w_compos,
              w_comlen.

  ENDMETHOD.
ENDCLASS.

No comments:

Post a Comment