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

How to Split formula

former_member242166
Participant
0 Likes
1,837

Dear Folks,

I have a requirement like we need to pass the values from selection screen. Also they will pass the Formula in the string based on the formula we need to calculate the result. How to achieve it kindly help me.

Example :

They will pass the values X=10, Y=20, Z=30 also they will pass the formula X*Y+Z, How we can calculate the values in our program.

Thanks,

Linga

1 ACCEPTED SOLUTION
Read only

rosenberg_eitan
Active Contributor
0 Likes
1,612

Hi,

Check CALL FUNCTION 'EVAL_FORMULA'

Some code:

REPORT  y_r_eitan_test_51_13.

SELECTION-SCREEN BEGIN OF BLOCK block04 WITH FRAME.

PARAMETERS: p_debug TYPE debug_flg .

SELECTION-SCREEN END OF BLOCK block04 .

START-OF-SELECTION .

  PERFORM at_start_of_selection .

*----------------------------------------------------------------------*

FORM at_start_of_selection .

  PERFORM do_eval_formula .

ENDFORM .                    "at_selection_screen_input

*----------------------------------------------------------------------*

FORM do_eval_formula .

  CONSTANTS: c_formula TYPE char50 VALUE '(A+B)/C' .

  WRITE: / c_formula , / .

  DATA: program TYPE syrepid.

  DATA: value TYPE f .

  DATA: result TYPE p DECIMALS 4   .

  program = sy-repid.

  DATA: my_sy_subrc TYPE sysubrc .

  IF 1 EQ 2 .

* For reference only the actual usage is done in function 'eval_formula'

    PERFORM assign_value

      USING

        'A'

      CHANGING

        value

        my_sy_subrc.

  ENDIF .

  CALL FUNCTION 'EVAL_FORMULA'

    EXPORTING

*     DEGREES                       = ' '

      formula                       = c_formula

      program                       = program

      routine                       = 'ASSIGN_VALUE'

   IMPORTING

     value                         = value

   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.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

  ENDIF.

  result = value .

  WRITE: / , / result .

ENDFORM .                    "do_EVAL_FORMULA

*----------------------------------------------------------------------*

FORM assign_value

  USING

    var_name

  CHANGING

    var_value

    my_sy_subrc  .

  CASE var_name .

    WHEN 'A' . var_value = '10.0' .

    WHEN 'B' . var_value = '20.0' .

    WHEN 'C' . var_value = '2.0' .

  ENDCASE .

  DATA: number TYPE p DECIMALS 2 .

  number = var_value .

  WRITE: / var_name , '=' , number  .

  my_sy_subrc = 0 .

ENDFORM .                    "assign_value

*----------------------------------------------------------------------*

6 REPLIES 6
Read only

roberto_vacca2
Active Contributor
0 Likes
1,612

Hi.

Replace values 'X','Y','Z' with input X, Y, Z. Is this your need?

There's "Replace" instruction  for this purpose.

Hope to help

Bye

Read only

former_member259807
Active Participant
0 Likes
1,612

There are multiple ways to solve this.

One way is to create a string on the screen where the user types the complete formula (like 10 * 20 + 30), and you can use the string statements (SPLIT/SHIFT/REPLACE/etc) to determine what needs to be calculated.

Another way is to create 3 fields X, Y and Z, plus a string for the formula and using the same string statements to determine what needs to be done.

Third option is to create 3 (or more) fields, plus radio buttons for the formula options.

And the fourth one, create a calculator in ABAP:

Create an ABAP Calculator | SCN

Calculator in Module Pool | SCN

Read only

0 Likes
1,612

Dear Wallagh,

Thanks for your reply. Actually the formula is dynamic. They will give any formula like X+Y+Z or X-Y-Z or X*Y/Z etc.... whatever formula they will give we need to calculate the result dynamically.

If possible could you please elaborate in detail

Thanks,

Linga

Read only

ThangaPrakash
Active Contributor
0 Likes
1,612

Try with REPLACE statement

Read only

rosenberg_eitan
Active Contributor
0 Likes
1,613

Hi,

Check CALL FUNCTION 'EVAL_FORMULA'

Some code:

REPORT  y_r_eitan_test_51_13.

SELECTION-SCREEN BEGIN OF BLOCK block04 WITH FRAME.

PARAMETERS: p_debug TYPE debug_flg .

SELECTION-SCREEN END OF BLOCK block04 .

START-OF-SELECTION .

  PERFORM at_start_of_selection .

*----------------------------------------------------------------------*

FORM at_start_of_selection .

  PERFORM do_eval_formula .

ENDFORM .                    "at_selection_screen_input

*----------------------------------------------------------------------*

FORM do_eval_formula .

  CONSTANTS: c_formula TYPE char50 VALUE '(A+B)/C' .

  WRITE: / c_formula , / .

  DATA: program TYPE syrepid.

  DATA: value TYPE f .

  DATA: result TYPE p DECIMALS 4   .

  program = sy-repid.

  DATA: my_sy_subrc TYPE sysubrc .

  IF 1 EQ 2 .

* For reference only the actual usage is done in function 'eval_formula'

    PERFORM assign_value

      USING

        'A'

      CHANGING

        value

        my_sy_subrc.

  ENDIF .

  CALL FUNCTION 'EVAL_FORMULA'

    EXPORTING

*     DEGREES                       = ' '

      formula                       = c_formula

      program                       = program

      routine                       = 'ASSIGN_VALUE'

   IMPORTING

     value                         = value

   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.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

  ENDIF.

  result = value .

  WRITE: / , / result .

ENDFORM .                    "do_EVAL_FORMULA

*----------------------------------------------------------------------*

FORM assign_value

  USING

    var_name

  CHANGING

    var_value

    my_sy_subrc  .

  CASE var_name .

    WHEN 'A' . var_value = '10.0' .

    WHEN 'B' . var_value = '20.0' .

    WHEN 'C' . var_value = '2.0' .

  ENDCASE .

  DATA: number TYPE p DECIMALS 2 .

  number = var_value .

  WRITE: / var_name , '=' , number  .

  my_sy_subrc = 0 .

ENDFORM .                    "assign_value

*----------------------------------------------------------------------*

Read only

0 Likes
1,612

Hi,


Below is an example.


*******


REPORT zleo.

DATA: result   TYPE f,

       v_result(16) TYPE p DECIMALS 2.

PARAMETERS: p_form(35) DEFAULT 'X*Y+Z',

             x(2) DEFAULT 3,

             y(2) DEFAULT 3,

             z(2) DEFAULT 2.

START-OF-SELECTION.

   REPLACE: 'X' WITH x INTO p_form,

            'Y' WITH y INTO p_form,

            'Z' WITH z INTO p_form.

   CALL FUNCTION 'EVAL_FORMULA'

     EXPORTING

       formula                 = p_form

     IMPORTING

       value                   = result

     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.

     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

   ENDIF.

   v_result = result .

   WRITE v_result.


Regards,