06-13-2013 6:39 AM
Dear Experts,
I have few mathematical operands stored in SAP custom fields. How do i use it for calculation?
Please take this as example.
ztest-field1 = '100'.
ztest-field2 = '*'.
ztest-field3 = N+n
Now my requirement is i need to pass numeric values to N & n. Since we have addition operand in between them values of N & n needs to be summed & then i need to have formula like below.
Ztotal = ztest-field1 ztest-field2 ztest-field3. (Here ztest-field2 will again be a mathematical operand * ). So that I will get total of field1 & field2.
Thanks & Regards,
Satty
06-13-2013 7:18 AM
Hi Sathish,
You may want to use the eval() function in Javascript, in ABAP the Javascript processor is wrapped in class is CL_JAVA_SCRIPT
Check out this useful link for an example.
http://zevolving.com/2011/10/javascript-in-abap/
Hope it helps! Cheers!
06-13-2013 7:09 AM
Hi Satish ,
If you want to perform Mathematical Operations to your fields declare them with integer characteristics.
data : num1 type int1,
num2 type int2,
result type int4.
result = num1 + num2.
write : result.
Thanks & Regards
Pavan.N
06-13-2013 7:24 AM
Hi Pavan,
My case is field value itself an operand which may be + or *. How will i calculate in this case.
Hope u understand. field1 will have value 100, field2 will value +, field3 will have value 200.
Now i need to get the total of field1 & field3 by using operand in field2.
Thanks
Satty
06-13-2013 7:18 AM
Hi Sathish,
You may want to use the eval() function in Javascript, in ABAP the Javascript processor is wrapped in class is CL_JAVA_SCRIPT
Check out this useful link for an example.
http://zevolving.com/2011/10/javascript-in-abap/
Hope it helps! Cheers!
06-13-2013 7:30 AM
Hi Sathish,
Where exactly are you stuck? If I understand you correctly, you have the operands and operators stored in a database table and you need to perform calculations based on them. That should be doable using IF/CASE statements.
Regards,
Akshat
06-13-2013 7:37 AM
Hello satish,
you can simply use CASE statement and get your result.
For example : -
Case ztest-field2.
when '*'.
ztotal = (ztest-field1) * (ztest-field3).
when '+'.
ztotal = (ztest-field1) + (ztest-field3).
when others.
endcase.
06-13-2013 7:39 AM
You can have a maximum of 4 operations
So...
CASE Field2. " [OPERATOR]
When '+'.
Result = field1 + Field3.
When '-'.
Result = field1 - Field3.
When '*'.
Result = field1 * Field3.
When '/'.
Result = field1 / Field3.
Endcase.
BR
06-13-2013 7:40 AM
Hi,
Read the first field contents in 3 different variables and a CASE.. ENDCASE to select the operation as per the operand( Its few in numbers). For the 3rd field is the format always like N+n, N*n, N-n.. Then i guess this can be read by offset and processed.
Can you also clarify the datatypes of the fields and the context.
Cheers,
Arindam
06-13-2013 7:43 AM
Hi Sathish,
Hope you are trying to archieve this
Ztotal = 100 * (N + n)
sample code
ztest-Field2 = N + n.
Ztotal = Ztest-field1 * Ztest-field2.
Thanks & Regards,
Meenachi.R
06-13-2013 8:01 AM
Thanks a lot Van Thao. You link was very much useful & it worked.
Cheers
Satty.