Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

adding leading zeros to the display data

Former Member
0 Likes
3,027

hi folks,

I have the character variable of size 9, if I get the value of the variable less than 9, I need to add leading zeros to it accordingly before displaying the numeric value stored in it. I tried to use the 'SHIFT' command for that it did not work out.

here is the code..

data: len type I,

amount type C,

addspace len type I.

len = strlen( amount ).

write: ' the length of the string',len.

if ( len < 9 ).

addspace = 9 - len.

write: addspace.

SHIFT amount BY addspace Places LEFT.

Thanks in advance.

hi folks,

I have the character variable of size 9, if I get the value of the variable less than 9, I need to add leading zeros to it accordingly before displaying the numeric value stored in it. I tried to use the 'SHIFT' command for that it did not work out.

here is the code..

data: len type I,

amount type C,

addspace len type I.

len = strlen( amount ).

write: ' the length of the string',len.

if ( len < 9 ).

addspace = 9 - len.

write: addspace.

SHIFT amount BY addspace Places LEFT.

Thanks in advance.

5 REPLIES 5
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,234

try using the conversion fm for alpha.



CONVERSION_EXIT_ALPHA_INPUT

Regards,

Rich Heilman

Read only

Former Member
1,234

Hi Santhosh,

Have you tried using a NUMC variable?

It always has leading zeros.

Code would be:


data: amount(9)      type c,
      numc_amount(9) type n.
* If char field only contains numeric data
if amount co '0123456789 '.  "Added a space in here!"
  numc_amount = amount.
endif.

* If the numc amount is filled.
if not numc_amount is initial.
*  Do something with it! (for example: WRITE)
   write: / 'Number is: ' numc_amount.   
endif. 

Hope that helps.

Brad

Message was edited by: Brad Williams

Read only

0 Likes
1,234

thanks a lot it helped. Brad.

Read only

Former Member
0 Likes
1,234

Hi Santhosh,

Use FM "CONVERSION_EXIT_MATN1_INPUT".

Pass the number to this FM . You will get zeroes prefixed to to it. Then take offset of output number.

example :

if you export g_num to FM and import l_num

then use. write l_num+9(9) to p_number.

in P_number you will get zero prefix as you want.

Regards,

Anand More.

Read only

Former Member
0 Likes
1,234

Hi Santhosh,

All the suggestions here will work. Make sure that your number is on the right justified to your character field. Here is an example.


DATA: v_char09_left_justified(09)  TYPE c,
      v_char09_right_justified(09) TYPE c,
      v_numc09(09)                 TYPE n.


START-OF-SELECTION.

*-- in case the value is left justified in the field
  v_char09_left_justified = '9        '.
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
       EXPORTING
            input  = v_char09_left_justified
       IMPORTING
            output = v_char09_left_justified.

  WRITE:/ 'V_CHAR09_LEFT_JUSTIFIED from FM =', v_char09_left_justified.

  v_numc09 = v_char09_left_justified.

  WRITE:/ 'V_NUMC09 =', v_numc09.

*-- in case the value is right justified in the field
  v_char09_right_justified = '        9'.
  SHIFT v_char09_right_justified LEFT DELETING LEADING space.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
       EXPORTING
            input  = v_char09_right_justified
       IMPORTING
            output = v_char09_right_justified.

 WRITE:/ 'V_CHAR09_RIGHT_JUSTIFIED from FM =', v_char09_right_justified.

  v_numc09 = v_char09_right_justified.

  WRITE:/ 'V_NUMC09 =', v_numc09.