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 zeros to a character string

Former Member
0 Likes
38,537

Hello friends,

I want to add leading zeros for a field.

the field is a character string.

for example ,

data: A(5) type c.

now when A = 'ab' (non-numeric value)

i want this to be converted in '000ab'

so, is there any standard Function module or any other way for doing this conversion ?

I tried the FM 'CONVERSION_EXIT_ALPHA_INPUT' but this FM does not work for non-numeric inputs..

Thanks.

1 ACCEPTED SOLUTION
Read only

Former Member
17,907

this will work

REPORT  ychatest.

DATA : v_char(5) TYPE c VALUE 'ab'.

SHIFT v_char RIGHT deleting trailing space.
translate v_char using ' 0'.
WRITE : v_char.

14 REPLIES 14
Read only

Former Member
0 Likes
17,907

Hi,

Do it like this,

data: A(5) type c.

data: b(5) type n.

a = 'AB'

unpack A to B.

Reward if useful!

Read only

0 Likes
17,907

Best simple option!

Read only

Former Member
0 Likes
17,907

use <b>OVERLAY A WITH '00000'.</b>

reward if useful..

Read only

Former Member
17,907

Hi,

The packed field is transported right-justified to the character field, if required with a

decimal point. The first position is reserved for the sign. Leading zeros appear as

blanks. If the target field is too short, the sign is omitted for positive numbers. If this is still not sufficient, the field is truncated on the left. ABAP indicates the truncation with an asterisk (*). If you want the leading zeros to appear in the character field, use UNPACK instead of MOVE.

UNPACK

Converts variables from type P to type C.

Syntax

UNPACK <f> TO <g>.

Unpacks the packed field <f> and places it in the string <g> with leading zeros. The opposite of PACK.

Regards,

Bhaskar

Read only

BerndDittrich
Product and Topic Expert
Product and Topic Expert
0 Likes
17,907

I just measured the different alternatives and unpack is clearly the fastest.

Read only

Former Member
17,908

this will work

REPORT  ychatest.

DATA : v_char(5) TYPE c VALUE 'ab'.

SHIFT v_char RIGHT deleting trailing space.
translate v_char using ' 0'.
WRITE : v_char.

Read only

0 Likes
17,907

Thanks for your support

Read only

0 Likes
17,907

Yes, It works.

SHIFT v_char RIGHT deleting trailing space. 
translate v_char using ' 0'.
Read only

Former Member
0 Likes
17,907

i think you cant add zero for non numeric value by std fn modules.

do like this.

data : a(5) value 'ab',

len type i.

compute len = strlen( text ).

len = 5 - len."since you have declared a(5)

do len times.

concatenate '0' a into a.

enddo.

regards

shiba dutta

Read only

Former Member
0 Likes
17,907

Hello,

Check this code. It's working for me.


DATA: char TYPE char5.
DATA: strglen TYPE i.
DATA: len TYPE i.


char = 'AB'.

len = STRLEN( char ).

strglen = 5 - len.

DO strglen TIMES.

  CONCATENATE '0' char INTO char.

ENDDO.

WRITE:/ char.

Regards,
Deepu.K

Read only

Former Member
0 Likes
17,907

shift A right deleting trailing space.

overlay A with '00000'.

Reward if useful.

Read only

Former Member
0 Likes
17,907

modified..

DATA: a(5) TYPE c.

<b>data: len1 type i,

len2 type i.

a = 'AB'.

len1 = strlen( a ).

len2 = 5 - len1.

move a0(len1) to alen2(len1).

clear a+0(len1).

overlay a with '00000'.

</b>

Read only

Former Member
17,907

Hi,

Check this FM CONVERSION_EXIT_NUMCV_INPUT.

Read only

ABAPMarty
Participant
0 Likes
15,190

The best way to do this, per the coding queen Jelena is with a string template:

 

DATA: lv_n_matnr TYPE matnr VALUE '517352'.

"Later:
DATA(lv_zp_matnr) = | { lv_n_matnr ALPHA = IN } | .

 

This takes care of the "shift right" and the zero padding in one step.

Test ABAP output:

ABAPMarty_0-1721343791584.png