2009 Aug 14 10:41 AM
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
2009 Aug 14 12:25 PM
>
> 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.
Hi,
Thanks for the response.
Is this the only way?
Any function module or method would not help us computing such formulae??
2009 Aug 14 10:46 AM
2009 Aug 14 10:47 AM
Hi,
Thanks for the response.
Is this the only way?
Any function module or method would not help us computing such formulae??
2009 Aug 14 10:55 AM
Hi
check these FM
FIMA_FORMEL_ANZEIGEN
FORMULA_AS_STRING
Hope it resolves your issue.
Thanks
Viquar Iqbal
2009 Aug 14 10:57 AM
Hello
I consider that this the most best decision for your purpose.
I distrust that exists FM or method for this.......
2009 Aug 14 10:51 AM
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
2009 Aug 14 10:57 AM
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.
2009 Aug 14 10:52 AM
hi friend try it through Define keywords ie macros. regards surender.s
2009 Aug 14 12:10 PM
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
2009 Aug 14 12:25 PM
>
> 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.
2009 Aug 17 9:58 AM
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
| User | Count |
|---|---|
| 6 | |
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |