Add leading zeros in between constant and variable

Viewed 2553

Any idea how to add zeros between sign Q and quantity?

I have sign Q and quantity as:

  • example 1: Q10
  • example 2: Q300

Code:

DATA variable TYPE c LENGTH 7 VALUE 'Q'

Expectation:

  • from example 1: Q000010
  • from example 2: Q000300

Thanks.

3 Answers

Many solutions. One of them (ABAP >= 7.02):

DATA(quantity) = 153.

DATA(variable) = |Q{ quantity WIDTH = 6 ALIGN = RIGHT PAD = '0' }|.

ASSERT variable = 'Q000153'.

NB:

You need to split numeric and alfa numeric part. Then you can use below code numeric part:

write lv_text to lv_text right-justified.
translate lv_text using ' 0'.

Also you can use CONVERSION_EXIT_ALPHA_INPUT function module for only number based variables.
Then concatenate two part.

Another possible solution is to use any QM DDIC data element in string templates utilizing its conversion like mkysoft suggested:

DATA qty TYPE vbeln VALUE '10' .

DATA(result) = 'Q' && CONV char6( |{ qty ALPHA = IN }| ). 
Related