Skip to Content

Convert string to integer

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

Add comment
10|10000 characters needed characters exceeded

2 Answers

  • Best Answer
    Posted on Jan 15, 2008 at 05:35 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Posted on Jan 15, 2008 at 05:40 PM
    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.
    
    

    Add comment
    10|10000 characters needed characters exceeded