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

converting string value to numeric

Former Member
0 Likes
2,174

hi

how to convert string value or character variable (that actually has numeric or floating value) to its original numeric value or floating value.

is there any function module

5 REPLIES 5
Read only

Former Member
0 Likes
1,911

Hi,

Condense the character string and assign the value to a variable declared as a numeric or floating value.

Rgds,

Subramanian

Read only

Former Member
0 Likes
1,911

Check this code:

data val type i.

***********************************************************************

  • SELECTION SCREEN *

***********************************************************************

PARAMETERS: lines(5) TYPE c obligatory.

START-OF-SELECTION.

try.

val = lines.

catch cx_root.

write: 'Conversion not possible'.

endtry.

Same can be done for float as well.

Regards,

Joy.

Read only

Former Member
0 Likes
1,911

Hello,

See the code bellow:


data:
  lv_c type c length 10,
  lv_p type p decimals 2.

lv_c = '123.456'.
CONDENSE lv_c NO-GAPS.
lv_p = lv_c.

Regards.

Read only

naimesh_patel
Active Contributor
0 Likes
1,911

You can achieve this by moving the String contents to the Numberic variables. Make sure that you catch the runtime exception when you don't have numeric value in your String variable.

Like:


DATA: L_STRING TYPE STRING,
      L_P      TYPE P DECIMALS 2.

DATA: L_CONV TYPE REF TO CX_SY_CONVERSION_NO_NUMBER,
      L_TEXT TYPE STRING.


L_STRING = '15.25'.

TRY.
    L_P = L_STRING.
  CATCH CX_SY_CONVERSION_NO_NUMBER INTO L_CONV.
    L_TEXT  = L_CONV->GET_TEXT( ).
    WRITE: / L_TEXT.
ENDTRY.

WRITE: / L_STRING,
       / L_P.



L_STRING = 'abc'.

TRY.
    L_P = L_STRING.
  CATCH CX_SY_CONVERSION_NO_NUMBER INTO L_CONV.
    L_TEXT  = L_CONV->GET_TEXT( ).
    WRITE: / L_TEXT.
ENDTRY.

WRITE: / L_STRING,
       / L_P.

Regards,

Naimesh Patel

Read only

0 Likes
1,911

juss declare a numeric variable...and move the value of character variable to this newly declared variable it will get converted... this is called 'type-casting'