cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert a character to number

Former Member
0 Kudos

hi,

i want to convert a character to number in abap which function can i use? and how to use this function?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

Sample code below:

DATA: lv_chr(4) type c,
      lv_num    type p.

CALL FUNCTION 'MOVE_CHAR_TO_NUM'
  EXPORTING
    CHR             = lv_chr
  IMPORTING
    NUM             = lv_num
  EXCEPTIONS
    CONVT_NO_NUMBER = 1
    CONVT_OVERFLOW  = 2
    OTHERS          = 3.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

former_member624107
Contributor
0 Kudos

PACK f TO g.

Places the character field f in packed format in the field g. Reverse of the UNPACK statement.

Example

DATA C_FIELD(4) TYPE C VALUE '0103',

P_FIELD(2) TYPE P.

PACK C_FIELD TO P_FIELD.

C_FIELD: C'0103' --> P_FIELD: P'103C'

Note

The field f can contain up to 16 characters.

Answers (7)

Answers (7)

Sergiu
Contributor
* https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-us/abenconversion_type_p.htm
data: c_field type c LENGTH 40 value '8.1499999999999995E-01'.
data: p_field type p DECIMALS 5.
p_field = CONV f( c ).

write: / c_field.
write: / p_field.
Former Member

CALL FUNCTION 'MOVE_CHAR_TO_NUM'

Former Member

Hi,

try this...

data : n type I.

data : C(20).

C = '356'.

move C to n.

n = n + 1.

write n.

former_member386202
Active Contributor

Hi,

Use Fms

MOVE_CHAR_TO_NUM

C14W_CHAR_NUMBER_CONVERSION

CHAR_NUMC_CONVERSION

Regards,

PRashant

Former Member

Hi,

Use the following Code :

DATA NO1(10) TYPE C VALUE '1234567890'.

DATA NO2 TYPE I.

NO2 = NO1.

WRITE NO2.

the foll code will convert the string to interger.

DATA NO1 TYPE STRING.

DATA NO2 TYPE I.

NO1 = '1234567890'.

NO2 = NO1.

WRITE NO2.

Also you can use the FM <b>CONVERT_STRING_TO_INTEGER,</b>.

Hope this would be helpful.

Regards,

Lalit

Former Member
0 Kudos

Hi

I am working in one PO enhancement.

Here in field i.e. charactertype fileld and storing as v_charvalue = '1,950 US GAL' ( v_Charvalue is type C).

Now I want to fetch only numeric value i.e 1950 into v_num ( this is type N ).

From the v_charvalu how will fetch only 1950 ....here it should exclude comma (,) and US GAL.

finally I need v_num = 1950

Pls guide me how to fetch only the numeric value.

sebastian_sap93
Explorer
0 Kudos

Dear all, thank you very much for your participation and help, which led me to your solutions.

Kind Regards

Torbjörn Saretz

former_member207703
Active Participant
0 Kudos

I think you didn't required any FM to do it.

Just assign c type variable to variable type P. It will work fine.

Like:

DATA : lv_ch(4) type c,

lv_num type p length 4.

lv_num = lv_ch.

write:/ lv_num.

It's just simply work in case of your problem.