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: 

convert text to amount

Former Member
0 Kudos

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

andreas_mann3
Active Contributor
0 Kudos

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

FredericGirod
Active Contributor
0 Kudos

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

andreas_mann3
Active Contributor
0 Kudos

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

0 Kudos

frederic, andreas,

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

ryan.

former_member182371
Active Contributor
0 Kudos

hi,

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

Best regards.