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

convert text to amount

Former Member
0 Likes
2,146

hi,

given this simple problem:

data: text(10), amount type p decimals 2.
text = '20,000.00'.

how do i pass the value of 20,000.00 to the amount variable without incurring ABAP runtime errors, besides manually removing the "," delimiters for each thousand?

(actually the amount field is type currency, but i know currency and packed are the same in nature)

ryan.

1 ACCEPTED SOLUTION
Read only

andreas_mann3
Active Contributor
0 Likes
1,030

Hello Ryan,

here's an example to catch/change the value:

REPORT z6.

DATA digit(11) VALUE ' 0123456789'.

PARAMETERS: text(16) DEFAULT '20,000.00'.

DATA c_text(16).

DATA amount TYPE p DECIMALS 2.

DATA n TYPE n.

DATA i TYPE i.

c_text = text.

CATCH SYSTEM-EXCEPTIONS conversion_errors = 1.

MOVE c_text TO amount.

ENDCATCH.

IF sy-subrc EQ 1.

CALL FUNCTION 'PREPARE_STRING'

EXPORTING

i_valid_chars = digit

i_xvalid_check = 'X'

i_xchar_repl = 'X'

i_xtoupper = 'X'

CHANGING

c_string = c_text.

CONDENSE c_text NO-GAPS.

DESCRIBE FIELD amount DECIMALS n.

i = 10 ** n.

amount = c_text / i.

ENDIF.

WRITE:/ text, 50 amount.

regards Andreas

4 REPLIES 4
Read only

FredericGirod
Active Contributor
0 Likes
1,030

Hello,

- If you don't want a short time error, use the command catch.

Ex :

catch system-exceptions conversion_errors = 1.

move ... to text.

endcatch.

if sy-subrc eq 1.

....

endif.

- I write always my own form to convert text amount to value amount with replace '.' by ',' ...

Regards

Frédéric

Read only

andreas_mann3
Active Contributor
0 Likes
1,031

Hello Ryan,

here's an example to catch/change the value:

REPORT z6.

DATA digit(11) VALUE ' 0123456789'.

PARAMETERS: text(16) DEFAULT '20,000.00'.

DATA c_text(16).

DATA amount TYPE p DECIMALS 2.

DATA n TYPE n.

DATA i TYPE i.

c_text = text.

CATCH SYSTEM-EXCEPTIONS conversion_errors = 1.

MOVE c_text TO amount.

ENDCATCH.

IF sy-subrc EQ 1.

CALL FUNCTION 'PREPARE_STRING'

EXPORTING

i_valid_chars = digit

i_xvalid_check = 'X'

i_xchar_repl = 'X'

i_xtoupper = 'X'

CHANGING

c_string = c_text.

CONDENSE c_text NO-GAPS.

DESCRIBE FIELD amount DECIMALS n.

i = 10 ** n.

amount = c_text / i.

ENDIF.

WRITE:/ text, 50 amount.

regards Andreas

Read only

0 Likes
1,030

frederic, andreas,

amazing help you've given me - thanks for all the input. i've showered you with stars already... 😃

ryan.

Read only

former_member182371
Active Contributor
0 Likes
1,030

hi,

you can use as well funtion module <b>HRCM_STRING_TO_AMOUNT_CONVERT</b>.

Best regards.