Conversion exit to manage percentages

Conversion exit to manage percentages

To set percentage value, displayed with the char. %, when you want to store only the number, you can use conversion exit.

Here I made the choice to store the number as a string, otherwise, default value is 0 and in my case, 0% is different from empty value.

Domain :

Notice Output lenght to 4 and use of convertion routine

Create 2 FM, for INPUT and OUTPUT :

In TOP include, declare the constant for character % :


  CONSTANTS : gc_percent TYPE char1 VALUE '%' ##NO_TEXT.

FM CONVERSION_EXIT_ZPER0_INPUT :


FUNCTION conversion_exit_zper0_input.
*"----------------------------------------------------------------------
*"*"Interface locale :
*"  IMPORTING
*"     VALUE(INPUT)
*"  EXPORTING
*"     VALUE(OUTPUT)
*"  EXCEPTIONS
*"      INVALID_INPUT
*"----------------------------------------------------------------------

  CHECK input IS NOT INITIAL.

  IF input CS gc_percent ##NO_TEXT.
    REPLACE ALL OCCURRENCES OF gc_percent IN input WITH ''.
  ENDIF.

  IF NOT input CO '0123456789 ' ##NO_TEXT.
    DATA(lv_flag_error) = abap_true.
  ENDIF.

  IF lv_flag_error IS INITIAL.
    TRY.
        DATA(lv_percent) = CONV to_int1( input ).

        IF lv_percent > 100.
          lv_flag_error = abap_true.
        ENDIF.

      CATCH cx_sy_conversion_no_number.
        lv_flag_error = abap_true.
      CATCH cx_sy_conversion_overflow.
        lv_flag_error = abap_true.
    ENDTRY.

  ENDIF.

  IF lv_flag_error = abap_true.

    MESSAGE e086(zdte) RAISING invalid_input.
    " Pourcentage : Merci de renseigner un nombre entier entre 0 et 100.

    output = input.
    RETURN.
  ENDIF.

  " --------------------------------------------------------
  " Le champ est géré en chaine de caractère, car sinon, la
  " valeur initiale serait 0. et 0% est différent de vide.
  " On est obligé d'aligner le chiffre sur la droite si on
  " veut que le tri sur champ texte fonctionne. On corrige
  " la saisie utilisateur pour que lors de recherches
  " (SELECT) sur ce champs, ça marche correctement.
  " --------------------------------------------------------
  IF lv_percent = 100.
    output = input.

  ELSEIF lv_percent >= 10.
    output = | { condense( input ) }|. " Ajout d'un espace

  ELSE.
    output = |  { condense( input ) }|. " Ajout de 2 espaces

  ENDIF.

ENDFUNCTION.

FM CONVERSION_EXIT_ZPER0_OUTPUT :


FUNCTION conversion_exit_zper0_output.
*"----------------------------------------------------------------------
*"*"Interface locale :
*"  IMPORTING
*"     VALUE(INPUT)
*"  EXPORTING
*"     VALUE(OUTPUT)
*"----------------------------------------------------------------------

  CHECK input IS NOT INITIAL.

  output = condense( input ) && gc_percent.

ENDFUNCTION.

About the author

fjourneau administrator

Hi, I'm Florian Journeau, SAP ABAP R3 Freelance, based in Toulouse, France. You want to know more about me, have a look on my CV : cv.fjourneau.net.

Leave a Reply