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 string to integer

Former Member
0 Likes
6,008

I need to convert a string (it is a packed number stored in a string variable) into an integer. I tried just saying integer = string but it throws a run-time error. Then I tried to use fm convert_string_to_integer but that FM isn't at all what it sounds like (it doesn't' accept a string of numbers). How can I do this?

Davis

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
3,488

what's the problem?



data: str type string value '123456'.
data: i type i.

i = str.

write:/ i.

what is the value in your string? Does it contain something other than numbers? If so you would need to get rid of those. For example, say that your string contained a number with a thousand separator like ','.



data: str type string value '123,456'.
data: i type i.

translate str using ', '.    "< get rid of the comma
condense str no-gaps.
i = str.

write:/ i.

Regards,

Rich Heilman

3 REPLIES 3
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
3,489

what's the problem?



data: str type string value '123456'.
data: i type i.

i = str.

write:/ i.

what is the value in your string? Does it contain something other than numbers? If so you would need to get rid of those. For example, say that your string contained a number with a thousand separator like ','.



data: str type string value '123,456'.
data: i type i.

translate str using ', '.    "< get rid of the comma
condense str no-gaps.
i = str.

write:/ i.

Regards,

Rich Heilman

Read only

0 Likes
3,488

Rich, you hit the nail on the head, I have commas and decimal points in the string. I will make the change which you posted.

Thanks a lot!

Davis

Read only

former_member194669
Active Contributor
0 Likes
3,488

report  zars no standard page heading
        line-size 170
        line-count 65(4).

data : int type i.
data : char(10) type c value '123'.

class cl_abap_container_utilities definition load.

call method cl_abap_container_utilities=>read_container_c
  exporting
    im_container           = char
  importing
    ex_value               = int
  exceptions
    illegal_parameter_type = 1
    others                 = 2.

write : int.

a®