Application Development 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: 

Need help regarding Arithmetic Expressions

Former Member
0 Kudos

Hi Experts,

I have a requirement where in I need to pass Arithmetic Expressions (+,-,/) as constants to a subroutine.For ex:

constants:c_p type char1 value '+',

c_m type char1 value '-',

data:v_a type char1 value '2',

v_b type char1 value '6',

v_r type string.

v_r = v_ a + v_b.

In the above instead of + I want use c_p.Please help me out is there any other way in defining Arithmetic expresiions where I could use constants or variables instead of directly using these expressions(+,-).Thanks in advance.

With Regards,

Srini..

10 REPLIES 10

SuhaSaha
Advisor
Advisor
0 Kudos

Hello Srini,

I think you must have a look at the FMs belonging to FuGr. CALC.

For e.g., EVAL_FORMULA which evaluates Literal or Variable formulas

CONSTANTS:
C_P TYPE CHAR1 VALUE '+',
C_M TYPE CHAR1 VALUE '-'.

DATA:
V_A TYPE CHAR1 VALUE '2',
V_B TYPE CHAR1 VALUE '6',
V_FORMULA TYPE STRING,
V_RES_F   TYPE F,
V_RES_P   TYPE P.

CONCATENATE V_A C_P V_B INTO V_FORMULA SEPARATED BY SPACE.

CALL FUNCTION 'EVAL_FORMULA'
  EXPORTING
    FORMULA                 = V_FORMULA
  IMPORTING
    VALUE                   = V_RES_F
  EXCEPTIONS
    DIVISION_BY_ZERO        = 1
    EXP_ERROR               = 2
    FORMULA_TABLE_NOT_VALID = 3
    INVALID_EXPRESSION      = 4
    INVALID_VALUE           = 5
    LOG_ERROR               = 6
    PARAMETER_ERROR         = 7
    SQRT_ERROR              = 8
    UNITS_NOT_VALID         = 9
    MISSING_PARAMETER       = 10
    OTHERS                  = 11.

IF SY-SUBRC = 0.
  MOVE V_RES_F TO V_RES_P.

  WRITE: V_RES_P .
ENDIF.

BR,

Suhas

Former Member
0 Kudos

Hey Suhas,

The FM u said will work fine but I am thinking for a solution where we can pass keywords or arthematic expressions dynamically.Anyways thanks a lot for your help you all deserve good score.I will update you all if I find any kind of solution.

Note:No macros.

Thanks and Regards,

Srini..

Former Member
0 Kudos

Hi Srini

You may write


ADD v_a v_b TO v_r.

Pushpraj

0 Kudos

>

> > ADD v_a v_b TO v_r.

I can not use this Statement in SAP. Are you using any RFC which incorporates C language syntax with ABAP?

Cheers

0 Kudos

HI Pushparaj,

ADD v_a v_b TO v_r is showing an error.Any other ways can I achieve this???

Thankis and Regards,

Srini...

0 Kudos

Hi

You could do


CLEAR v_r.
ADD v_a TO v_r.
ADD v_b TO v_r.

Pushpraj

0 Kudos

I can not use this Statement in SAP. Are you using any RFC which incorporates C language syntax with ABAP?

Ya my bad!

0 Kudos

How about this?


data: num1 type i,
      num2 type i,
      num3 type i.

num1 = '7'.

num2 = '8'.

add num1 then num2 until num3 giving num3.


write: num3.

Vikranth

0 Kudos

Hello ,

I think you can check the FM: EVAL_FORMULA?

What's the problem using that FM?

Suhas

former_member184551
Contributor
0 Kudos

Are you trying to open and close folders in a basic list?