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

Execute formula in a string

Former Member
0 Likes
3,077

Hi Gurus,

I have a formula inside a string like given below.


DATA: lv_string TYPE c LENGTH 100,
            lv_result TYPE p decimals 2.

lv_string = '2 * 3 * 4'.

i want to run/execute the formula in LV_STRING and move result to LV_RESULT. Please help me how to do it. The formula given in LV_STRING could be completely dynamic in ABAP arithmetic sytax.

Please help.

Thanks

Srini

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,753

>

> Hi Gurus,

>

> I have a formula inside a string like given below.

>

>


> DATA: lv_string TYPE c LENGTH 100,
>             lv_result TYPE p decimals 2.
> 
> lv_string = '2 * 3 * 4'.
> 

>

> i want to run/execute the formula in LV_STRING and move result to LV_RESULT. Please help me how to do it. The formula given in LV_STRING could be completely dynamic in ABAP arithmetic sytax.

>

> Please help.

>

> Thanks

> Srini

Hello Srini

Please check if the FM 'EVAL_FORMULA' can help you.

Regards

Rajesh.

10 REPLIES 10
Read only

Former Member
0 Likes
1,753

Hello

Check this link:

You will have solution.

Read only

0 Likes
1,753

Hi,

Thanks for the response.

Is this the only way?

Any function module or method would not help us computing such formulae??

Read only

0 Likes
1,753

Hi

check these FM

FIMA_FORMEL_ANZEIGEN

FORMULA_AS_STRING

Hope it resolves your issue.

Thanks

Viquar Iqbal

Read only

0 Likes
1,753

Hello

I consider that this the most best decision for your purpose.

I distrust that exists FM or method for this.......

Read only

Former Member
0 Likes
1,753

Hi,

Remove single quots, if you put anything in single quots it will take as a string, your formula will not execute.

follow this code

lv_string = 2 * 3 * 4.

lv_result = lv_string.

Regards,

Sunaina Reddy T

Read only

0 Likes
1,753

Hi Sunaina,

I do get formula in string only, i cannot remove single quotes. I get the formula through an interface which is taken into a string, and should be executed it.

Read only

Former Member
0 Likes
1,753

hi friend try it through Define keywords ie macros. regards surender.s

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,753

also check this


DATA: l_string(128) TYPE c,
      res TYPE i,
      ope TYPE c,
      c TYPE i,
      length TYPE i.


FIELD-SYMBOLS: <fs> TYPE ANY.
FIELD-SYMBOLS: <fs1> TYPE ANY.

l_string = '1 + 2 + 3'.
CONDENSE l_string NO-GAPS.
length = STRLEN( l_string ) + 1.
c = 0.

DO.
  IF sy-index = length.
    EXIT.
  ENDIF.
  ASSIGN l_string+c(1) TO <fs>.
  ASSIGN res TO <fs1>.
  IF <fs> CA '1234567890'.
    IF sy-index = 1.
      <fs1> = <fs> * 1.
    ELSE.
      CASE ope.
        WHEN '+'.
          <fs1> = <fs1> + <fs>.
        WHEN '*'.
          <fs1> = <fs1> * <fs>.
        WHEN '-'.
          <fs1> = <fs1> - <fs>.
      ENDCASE.
    ENDIF.
  ELSE.
     ope = <fs>.
      c = c + 1.
      CONTINUE.
  ENDIF.
  c = c + 1.
ENDDO.

WRITE res.

this works only for single digit no ... apply your logic if u need.

do go for the thread linkmgiven by meroz

Edited by: Keshu Thekkillam on Aug 14, 2009 4:45 PM

Read only

Former Member
0 Likes
1,754

>

> Hi Gurus,

>

> I have a formula inside a string like given below.

>

>


> DATA: lv_string TYPE c LENGTH 100,
>             lv_result TYPE p decimals 2.
> 
> lv_string = '2 * 3 * 4'.
> 

>

> i want to run/execute the formula in LV_STRING and move result to LV_RESULT. Please help me how to do it. The formula given in LV_STRING could be completely dynamic in ABAP arithmetic sytax.

>

> Please help.

>

> Thanks

> Srini

Hello Srini

Please check if the FM 'EVAL_FORMULA' can help you.

Regards

Rajesh.

Read only

0 Likes
1,753

Hi,

The function module works now, thank you.

I have done a sample code, and may be useful for others....


REPORT YTESTCALC.
PARAMETERS: FORMEL(50) OBLIGATORY.
DATA: RETCODE      LIKE SY-SUBRC,
      FUNCNAME(30) TYPE C,
      MESSAGE(70)  TYPE C,
      POS          TYPE I,
      C            TYPE I.

*  FORMEL = '(2 + 3) * 4'.
* Formel FORMEL auf syntaktische Korrektheit prüfen
  CALL FUNCTION 'CHECK_FORMULA'
       EXPORTING  FORMULA   = FORMEL
       IMPORTING  SUBRC     = RETCODE
                  FUNCNAME  = FUNCNAME
                  MESSAGE   = MESSAGE
                  POS       = POS.

  IF RETCODE IS INITIAL.
*    Wenn Formel FORMEL syntaktisch korrekt ist, auswerten
     CALL FUNCTION 'EVAL_FORMULA'
          EXPORTING  FORMULA   = FORMEL
          IMPORTING  VALUE     = C
          EXCEPTIONS OTHERS    = 1.
     IF SY-SUBRC = 0.
        WRITE: / TEXT-001, C.
     ELSE.
        WRITE: / SY-SUBRC.
     ENDIF.
  ELSE.
     WRITE: / FUNCNAME, MESSAGE, POS.
  ENDIF.

-Pavan