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 character to numeric

Former Member
0 Likes
7,537

If I have a character field that contains only numbers how do I convert this so it moves into a numeric field. I thought it would accept it automatically if it had only numbers but we get a mismatch.

Can I just create another datatype like this

a type char.

b type numeric.

b= a

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,818

If it does not work try the instruction MOVE :

MOVE a TO b.

Mathieu

9 REPLIES 9
Read only

Former Member
0 Likes
2,819

If it does not work try the instruction MOVE :

MOVE a TO b.

Mathieu

Read only

Former Member
0 Likes
2,818

Hi,

try this

data: a(10) type c,

b(10) type n.

a = '123'.

b = a.

write:/ b.

b = 0000000123.

reward points if helpful,

thanks & regards,

venkatesh

Read only

0 Likes
2,818

what if I wnat it to have 3 decimals. how would that work? It is 17 places

Read only

Former Member
0 Likes
2,818

hi,

while converting one data type into another data type you must maintain the same length.....

data: par1(10) type c value '1234',
        par2 type n.

  MOVE para1 TO para2.

if the source field para1 contains any decimal values in that case you declare target variable of type Integer or Packed type.

The source field must contain the representation of a decimal number, that is, a sequence of digits with an optional sign and no more than one decimal point. The source field can contain blanks. If the target field is too short, an overflow may occur. This may cause the system to terminate the program.

DATA:    par1(10) type c value '1234',
             par2 type i.    " 

  MOVE para1 TO para2.

<b>or</b>

DATA:  par1(10) TYPE C,
           par2       TYPE P DECIMALS 2. " specifies the decemal positions
     
         MOVE par1 TO par2. 

regards,

Ashok Reddy

Read only

Former Member
0 Likes
2,818

u r going right..

just make sure.. to have same size for both variable..


DATA:   v_char(10) type c,
            v_numc(10) type c.

v_char = '1234'.

v_numc = v_char.

write:/ v_numc.

Result will be :

0000001234

Reward useful answers

Regards

Pradeep

Read only

Former Member
0 Likes
2,818

Hi,

Use the standard function module MOVE_CHAR_TO_NUM

Regards

Sudheer

Read only

0 Likes
2,818

How do I get this to then be a packed with decimials

Read only

ferry_lianto
Active Contributor
0 Likes
2,818

Hi,

Please try FM HRCM_STRING_TO_AMOUNT_CONVERT.

Regards,

Ferry Lianto

Read only

former_member194797
Active Contributor
0 Likes
2,818

try this program.

(invert . and , if you are using period as decimal point)

DATA: zchar(15) VALUE '123.456.789,12'.
DATA: znum(8) TYPE p DECIMALS 3.
CALL FUNCTION 'MOVE_CHAR_TO_NUM'
  EXPORTING
    chr             = zchar
  IMPORTING
    num             = znum
  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.
  WRITE: / 'error', sy-subrc.
ENDIF.

WRITE: / 'znum =', znum.