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

Problem in dividing the value

Former Member
0 Likes
3,581

Hi,

I have a problem in dividing the values, say example i want to divid n by n1.

n = 1339.80

n1 =1914

n2 = n/n1

but i am getting the output value as 1 and i tried the DIVIDE statement as well, but still i am facing the same proble.

Please help to solve this problem

or

Let me know the FM to calculate the percentage value.

Regards,

Vijay

Hi,

I have a problem in dividing the values, say example i want to divid n by n1.

n = 1339.80

n1 =1914

n2 = n/n1

but i am getting the output value as 1 and i tried the DIVIDE statement as well, but still i am facing the same proble.

Please help to solve this problem

or

Let me know the FM to calculate the percentage value.

Regards,

Vijay

15 REPLIES 15
Read only

Former Member
0 Likes
2,870

try declaring all the three variables as type P (Packed decimals) and perform the division.

Read only

0 Likes
2,870

the problem may be that the variable n2 is int type.

if you want an answer in decimal format then just make this variable of type p(packed decimal) and i you want an integer output then in sap 5/10 is 1 not 0.

use ceil or other similar commands.

Read only

0 Likes
2,870

Hi,

I try using the one you suggested as well and all other ways. but i am not getting the solution.

Is there any FM to calculate the percentage.

Regards,

Vijay

Read only

0 Likes
2,870

Try this.



Data: n type p decimals 5,
      n1 type p decimals 5,
      n2 type p decimals 5.

n = '1339.80'.
n1 = 1914.

n2 = n / n1.

write: n2.

Read only

0 Likes
2,870

It gives percentage value

data: n1 type p decimals 2,
      n2 type p decimals 2,
      n3 type p decimals 2.

  n1 = '1339.80'.
  n2 = 1914.

  n3 = n1 / n2 * 100.

  write:/ n3.

And what's the error your getting.

thanks\

Mahesh

Read only

Former Member
0 Likes
2,870

Hi,

Take the Type of the N2 as Decimal or Packet.

Read only

Former Member
0 Likes
2,870

Hi,

What is the data type of n and n1?

Read only

Former Member
0 Likes
2,870

seems like you have declared you variables of type i.

Read only

former_member222860
Active Contributor
0 Likes
2,870

Check this:

data: n1 type p decimals 2,
      n2 type p decimals 2,
      n3 type p decimals 2.

  n1 = '1339.80'.
  n2 = '1914.00'.

  n3 = n1 / n2.

  write:/ n3.

thanks\

Mahesh

Read only

Former Member
0 Likes
2,870

hi,

Declare it of type p and after division you can store in the data type of your choice.

Read only

Former Member
0 Likes
2,870

Hi,

Please check the following code, it is working fine.

PARAMETERS:n type p decimals 2 DEFAULT '1339.80',

n1 type p decimals 2 default '1914',

n2 TYPE p DECIMALS 2 .

n2 = n / n1.

WRITE n2.

Read only

Former Member
0 Likes
2,870

Hi all,

When i try hard code the value with the solution given is working. but the problem in my case is

DATA: n TYPE p DECIMALS 2,

n1 TYPE p DECIMALS 2,

n2 TYPE p DECIMALS 2,

n3(10) type c value '1339.80',

n4(15) type numc valu '1914'.

Note for n3 and n4 is hardcode for examplet, but in real time , the value will be passed from the field directly.

n = n3 .

n1 = n4.

n2 = n/ n1.

write n2.

Please check and let me know the solution for this case.

Regards,

Vijay

Read only

0 Likes
2,870

Hi,

I have tested the code and it is giving the output as 0.70.(i.e. N2 hold 0.70 value)

Read only

0 Likes
2,870

Hi,

Yes its coming correct if you pass the value without decimal for n4 and the field type CURR.Please find the logic below

DATA: n TYPE p DECIMALS 2,

n1 TYPE p DECIMALS 2,

n2 TYPE p DECIMALS 2,

n3(10) type c value '1339.80',

n4 type netwr value '1914.50'.

Note for n3 and n4 is hardcode for examplet, but in real time , the value will be passed from the field directly.

n = n3 .

n1 = n4.

n2 = n/ n1.

write n2.

Kindly check and help me out to solve this.

Regards,

vijay

Read only

Former Member
0 Likes
2,870

Hi all,

Here is the solution

data: n1 type p decimals 2,

n2 type p decimals 2,

n3 type p decimals 2.

n1 = '1339.80'.

n2 = '1914.50'.

n2 = n2 * 10.

n2 = n2 /1000.

n3 = n1 / n2.

write:/ n3.

then it will work correct in the case of the field has different type like CURR,CHAR,INTE,etc.

Any way thanks a lot for all.

Vijay