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

Executing an Arithmatic Formula stored as String

Former Member
0 Likes
681

Hi All,

Please help,

I have similar problem with Mr. Vijayakumar V.

I need to get result value of an arithmatic formula which stored as string.

ex:

zform = '( A / 2 ) + ( B * 1.25 + 2 )'.

if I have A = 10, and B = 4.

I do expect the final result to be : 12

for ( 5 + 7 ) = 12

Is there any simple way to do this or

Is there built in function in ABAP to deal with something like this?

Thank you,

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
658

Hi Oki,

You can solve your problem by generating subroutine at runtime.

Please look at sample code below:


REPORT  z_jr05.

DATA: code(72) OCCURS 10,
      prog(8),
      msg(120),
      lin(3),
      wrd(10),
      off(3),
      var_string(50),
      p_result  TYPE i.

var_string  = 'temp_result = ( A * B ) + 199.'.
REPLACE ALL OCCURRENCES  OF'A' IN var_string WITH '10'.
REPLACE ALL OCCURRENCES  OF'B' IN var_string WITH '11'.

APPEND 'PROGRAM SUBPOOL.'                  TO code.
APPEND 'FORM HITUNG changing p_result.'    TO code.
APPEND 'data: temp_result type i.'         TO code.
APPEND  var_string                         TO code.
APPEND 'p_result = temp_result.'           TO code.
APPEND 'ENDFORM.'                          TO code.

GENERATE SUBROUTINE POOL code NAME prog
        MESSAGE msg LINE lin
        WORD wrd    OFFSET off.

IF sy-subrc <> 0.
  WRITE: / 'Error during generation in line', lin,
  / msg,
  / 'Word:', wrd, 'at offset', off.
ELSE.
  WRITE: / 'The name of the subroutine pool is', prog.
  SKIP 2.
  PERFORM hitung IN PROGRAM (prog) CHANGING p_result.
  WRITE p_result.
ENDIF.

Regards,

4 REPLIES 4
Read only

Former Member
0 Likes
658

hi,

do this way ...


data : zform type i.

zform = ( A / 2 ) + (  (B * 1.25)  + 2 ).

Read only

Former Member
0 Likes
658
data : zform type p decimals 2,a type p decimals 2,
    b type p decimals 2.

define name.

  a =  &1 / 2.
  b  = &2 * '1.25'.
  b = b + 2.
  zform = a + b.


  write zform.

end-of-definition.

name 10 4.

pass the value like 10 4.

Regards,

V.Balaji

Reward if Usefull...

Edited by: Balaji V on Apr 3, 2008 7:30 AM

Edited by: Balaji V on Apr 3, 2008 7:31 AM

Read only

Former Member
0 Likes
658

hi,

do this way ...

data : zform type p decimals 2.

because if decimals places also there u need to collect with zero errors...

..
zform = ( A / 2 ) + (  (B * 1.25)  + 2 ).
...

Read only

Former Member
0 Likes
659

Hi Oki,

You can solve your problem by generating subroutine at runtime.

Please look at sample code below:


REPORT  z_jr05.

DATA: code(72) OCCURS 10,
      prog(8),
      msg(120),
      lin(3),
      wrd(10),
      off(3),
      var_string(50),
      p_result  TYPE i.

var_string  = 'temp_result = ( A * B ) + 199.'.
REPLACE ALL OCCURRENCES  OF'A' IN var_string WITH '10'.
REPLACE ALL OCCURRENCES  OF'B' IN var_string WITH '11'.

APPEND 'PROGRAM SUBPOOL.'                  TO code.
APPEND 'FORM HITUNG changing p_result.'    TO code.
APPEND 'data: temp_result type i.'         TO code.
APPEND  var_string                         TO code.
APPEND 'p_result = temp_result.'           TO code.
APPEND 'ENDFORM.'                          TO code.

GENERATE SUBROUTINE POOL code NAME prog
        MESSAGE msg LINE lin
        WORD wrd    OFFSET off.

IF sy-subrc <> 0.
  WRITE: / 'Error during generation in line', lin,
  / msg,
  / 'Word:', wrd, 'at offset', off.
ELSE.
  WRITE: / 'The name of the subroutine pool is', prog.
  SKIP 2.
  PERFORM hitung IN PROGRAM (prog) CHANGING p_result.
  WRITE p_result.
ENDIF.

Regards,