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: 

Use Operands + or * for calculation

sathish_kumar92
Explorer
0 Kudos

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

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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!

9 REPLIES 9

Former Member
0 Kudos

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

0 Kudos

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

Former Member
0 Kudos

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!

kakshat
Advisor
Advisor
0 Kudos

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

Former Member
0 Kudos

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.


Former Member
0 Kudos

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

arindam_m
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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

sathish_kumar92
Explorer
0 Kudos

Thanks a lot Van Thao. You link was very much useful & it worked.

Cheers

Satty.