Application Development 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: 

Get First 4 digits from a field

atharva21k
Explorer

i Want to get the first 4 digits of the number in have please refer to the following code.

w_1 LIKE PRCD_ELEMENTS-kbetr, (20.000000000)

w_2(5) TYPE c,

MOVE w_1 TO w_2.

REPLACE '.' WITH ',' INTO w_taux2.

so the output expected is 20.00

1 ACCEPTED SOLUTION

lemoreno
Participant
0 Kudos

Hi atharva kate,

Do you need to get the value with 2 decimals only?

If that is the situation, you can do this:

- Create a variable with type p decimals 2.

- Assign the value to the new variable.

You are going to get 20.00.

- Create a new variable TYPE string.

- Assign the last value to the string var.

- Use the replace statement.

8 REPLIES 8

0 Kudos

You just assign like this

w_2 = w_1+0(5).

result:

w_2 = 20.00

I think it will solve your problem

0 Kudos

w_1 is a pack decimal format

W_2 is a char

Sandra_Rossi
Active Contributor
0 Kudos

Please format your code like that:

DATA:
w_1 LIKE PRCD_ELEMENTS-kbetr, (20.000000000)
w_2(5) TYPE c,

MOVE w_1 TO w_2.

REPLACE '.' WITH ',' INTO w_taux2.

(Please edit your question, select your code and press the button [CODE], which makes the code appear colored/indented, it will be easier for people to look at it. Thank you!)

Sandra_Rossi
Active Contributor

Note that first 3 of 4 lines of your code are OBSOLETE. You should use TYPE instead of LIKE, LENGTH should be used, MOVE is obsolete.

DATA:
w_1 TYPE PRCD_ELEMENTS-kbetr, (20.000000000)
w_2 TYPE c LENGTH 5,

w_2 = w_1.

atharva21k
Explorer
0 Kudos

It is recommended to keep LIKE

Sandra_Rossi
Active Contributor
0 Kudos

Concerning TYPE/LIKE, when you use ABAP Objects (subroutines are obsolete), you get syntax error:

Only use "TYPE" to refer to ABAP Dictionary types, not "LIKE" or "STRUCTURE".

matt
Active Contributor

Oh, and to get data in an output format, use WRITE. Don't manipulate strings yourself.

lemoreno
Participant
0 Kudos

Hi atharva kate,

Do you need to get the value with 2 decimals only?

If that is the situation, you can do this:

- Create a variable with type p decimals 2.

- Assign the value to the new variable.

You are going to get 20.00.

- Create a new variable TYPE string.

- Assign the last value to the string var.

- Use the replace statement.