‎2016 Feb 15 11:53 AM
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
‎2016 Feb 15 12:42 PM
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
*----------------------------------------------------------------------*
‎2016 Feb 15 12:00 PM
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
‎2016 Feb 15 12:19 PM
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
‎2016 Feb 15 12:29 PM
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
‎2016 Feb 15 12:36 PM
‎2016 Feb 15 12:42 PM
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
*----------------------------------------------------------------------*
‎2016 Feb 15 12:57 PM
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,