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: 

converting string value to numeric

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi,

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

Rgds,

Subramanian

Former Member
0 Kudos

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.

Former Member
0 Kudos

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.

naimesh_patel
Active Contributor
0 Kudos

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

0 Kudos

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'