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

rounding issue

Former Member
0 Likes
1,453

Hello friends,

Rounding issue.

Hello friends, I am having a problem with rounding issues.

Lets say I have 500 as quantity. Now I split 500 into three quantities. Lets say when I do the split it splits into 100.6, 100.6, 298.8.

So when I am rounding it becomes 101, 101, 299 and now the total is 501, and is different from available.

Can any one suggest how I can handle this issue? I want 500 even after the split.

Ster.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,402

Hi,

Check this example...Try giving different values in v_menge & v_times..

You will get the split quantity in the internal table T_QTY.


DATA: t_qty   TYPE STANDARD TABLE OF int4 WITH HEADER LINE.
DATA: v_menge TYPE menge_d.
DATA: v_times TYPE n.
DATA: v_sum   TYPE int4.

v_menge = '200'.               " Input quantity
v_times = 3.                     " Input times

t_qty = v_menge / v_times.

* Append the internal table with same quantity.
DO v_times TIMES.

  APPEND t_qty.

ENDDO.

* Calculate the sum of the quantity in the internal table.
LOOP AT t_qty.
  v_sum = t_qty + v_sum.
ENDLOOP.

* If the sum is greater adjust the difference in the last record
IF v_sum > v_menge.
  v_sum = v_sum - v_menge.

  READ TABLE t_qty INDEX v_times.

  t_qty = t_qty - v_sum.

  MODIFY t_qty INDEX v_times.

* If the sum is lesser add the difference in the last record.
ELSEIF v_sum < v_menge.

  v_menge = v_menge - v_sum.

  READ TABLE t_qty INDEX v_times.

  t_qty = t_qty + v_menge.

  MODIFY t_qty INDEX v_times.

ENDIF.

BREAK-POINT.

Thanks,

Naren

11 REPLIES 11
Read only

Former Member
0 Likes
1,402

Hi Ster,

Try declaring the data type as Float.

Reward points if helpful.

Thanks

Aneesh.

Read only

0 Likes
1,402

Thanks Aneesh and Seshu.

I will try.

Ster.

Read only

Former Member
0 Likes
1,402

are you using data type Packed ? if so try to use Float Data type.

Read only

ferry_lianto
Active Contributor
0 Likes
1,402

Hi,

Please try this.


DATA: NUM1 TYPE F,
      NUM2 TYPE I.
                                                 
NUM1 = 500 / 3.
NUM1 = NUM1 * 3.
MOVE NUM1 TO NUM2.
                                                 
WRITE:/ NUM1, NUM2.

Regards,

Ferry Lianto

Read only

0 Likes
1,402

Hi all,

let's see this example

Available: 200

200/3 = 66.6

first 2 round in normal way.

then it will be 67 + 67 = 134

for the 3rd one... if decimal is less than .5 then use CEIL to round OR if the decimal id greater that .5 then use FLOOR to round that.

so add 134+66 = 200

I think it will solve your problem

Thanks and Regards

Pavan Kothapalli

Read only

0 Likes
1,402

Thanks everyone for the suggestion.

Aneesh, Seshu.

the method you guys suggested works fine but the output comes with decimals. the output cane be decimals has to be full values with no decimals.

Pavan, how do i know the last record i will processing, because it can be split into 3 or 4 any number.

ANy more suggestions.

Read only

0 Likes
1,402

Nevermind, I misread.

Message was edited by:

Matt Nagel

Read only

0 Likes
1,402

Move the decimal value to Intger field,now you will get no decimals.

Read only

0 Likes
1,402

Hi Ster,

Declare another variable as integer and at last when you r done with all the splitting and stuff, pass the output to this variable.

Reward points if useful.

Thanks

Aneesh.

Read only

Former Member
0 Likes
1,403

Hi,

Check this example...Try giving different values in v_menge & v_times..

You will get the split quantity in the internal table T_QTY.


DATA: t_qty   TYPE STANDARD TABLE OF int4 WITH HEADER LINE.
DATA: v_menge TYPE menge_d.
DATA: v_times TYPE n.
DATA: v_sum   TYPE int4.

v_menge = '200'.               " Input quantity
v_times = 3.                     " Input times

t_qty = v_menge / v_times.

* Append the internal table with same quantity.
DO v_times TIMES.

  APPEND t_qty.

ENDDO.

* Calculate the sum of the quantity in the internal table.
LOOP AT t_qty.
  v_sum = t_qty + v_sum.
ENDLOOP.

* If the sum is greater adjust the difference in the last record
IF v_sum > v_menge.
  v_sum = v_sum - v_menge.

  READ TABLE t_qty INDEX v_times.

  t_qty = t_qty - v_sum.

  MODIFY t_qty INDEX v_times.

* If the sum is lesser add the difference in the last record.
ELSEIF v_sum < v_menge.

  v_menge = v_menge - v_sum.

  READ TABLE t_qty INDEX v_times.

  t_qty = t_qty + v_menge.

  MODIFY t_qty INDEX v_times.

ENDIF.

BREAK-POINT.

Thanks,

Naren

Read only

0 Likes
1,402

Thanks All,