How to Remove Preceeding Zeros in ABAP Development
I have developed a simple ABAP report / ABAP program which displays the contents of an SAP table on screen and writes the total number of lines or total number of records in the related SAP table on the screen.
The output of the ABAP program where I write the lines number of the sample SAP table SAPLANE is as follows :
As seen in the above screenshot, for a better result we have to remove the preceeding zeros using various methods in ABAP.
Sample ABAP Code for Removing Preceeding Zeros
Here is the source code of an ABAP program which requires to be corrected in order to display the lines count without preceeding zeros.
REPORT ZTEST_SAPLANE.
DATA:
gs_saplane TYPE saplane,
gt_saplane TYPE TABLE OF saplane,
gv_lin TYPE n LENGTH 5,
gv_txt TYPE string.
SELECT * FROM saplane
INTO TABLE gt_saplane.
DESCRIBE TABLE gt_saplane LINES gv_lin.
CONCATENATE 'Table SAPLANE has' gv_lin 'number of lines'
INTO gv_txt SEPARATED BY space.
WRITE / gv_txt.
EXIT.
Remove Preceeding Zeros using SHIFT LEFT DELETING LEADING
Calling the SHIFT ... LEFT DELETING LEADING '0' is a nethod that can be implemented in ABAP codes in order to delete the zeros before the integer part of the variable.
And the solution is as simple as using the SHIFT ... LEFT DELETING LEADING '0' ABAP code before CONCATENATE command call.
* Remove preceeding zeros
SHIFT gv_lin LEFT DELETING LEADING '0'.
Remove Preceeding Zeros by Calling CONVERSION_EXIT_ALPHA_OUTPUT funcion
To call the CONVERSION_EXIT_ALPHA_OUTPUT function is an other solution for removing preceeding zeros.
Here is how the function CONVERSION_EXIT_ALPHA_OUTPUT is called with parameters.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = gv_lin
IMPORTING
output = gv_lin.