‎2016 Aug 17 3:52 PM
Hi,
I've got a problem with dividing, I need to divide 1 USD per 3 people. Each of them will receive 0.33USD, but I want to split whole amount, to add extra 0,01USD to someone, or take this one cent if I want to split 2USD per 3 people.
How can I split some amount into almost equal parts?
I guess, that pricing can do it (I saw once 0,33 0,33 0,34), but I don't know how to put there a fraction.
‎2016 Aug 17 5:52 PM
Not really an ABAP problem! The solution is very simple. After the calculations are done, you must calculate the sum of all division results (0.99) and subtract it from the initial total (1.00). You'll get 0.01, you just add it to the last "bucket" so that now the sum of all divisions corresponds to the initial total.
‎2016 Aug 18 12:41 AM
‎2016 Aug 17 8:06 PM
Hello Adam,
in good, old ABAP4, something like this will do the trick:
REPORT zfc_test.
PARAMETERS: p_amount TYPE p DECIMALS 2,
p_people TYPE n.
DATA: l_value TYPE p DECIMALS 2,
l_remainder TYPE p DECIMALS 2.
START-OF-SELECTION.
l_value = p_amount / p_people.
l_remainder = p_amount - ( l_value * p_people ).
DO p_people TIMES.
IF sy-index = p_people.
l_value = l_value + l_remainder.
ENDIF.
WRITE: / l_value.
ENDDO.
Here the result with 1 USD, 3 people:
here for 2 USD, 3 people:
Thanks and regards,
Flavio
‎2016 Aug 17 10:53 PM
Hi, of course this is not even tricky to develop, but what if I have 12 to divide by 7 people? There should be each 1.71, 0.03 missing. I want to split 0,03, not to add to some person. It's a little bit harder, but still possible to write in basic code, but that's not the point. I'm looking for some standard functionality, which do the same.
‎2016 Aug 18 7:06 AM
Highly commendable. If you find some standard functionality, please post it. I've never found any!
‎2016 Aug 18 7:23 AM
This method will give all the remainder to last person, I prefer to share it more ethically?
PARAMETERS: amount TYPE p DECIMALS 2,
people TYPE i.
START-OF-SELECTION.
DATA: share TYPE p DECIMALS 2.
DO people TIMES.
share = amount / people.
WRITE: / share.
SUBTRACT share FROM amount.
SUBTRACT 1 FROM people.
ENDDO.
So for 3 or 7people
0,33 0,14
0,34 0,14
0,33 0,14
0,15
0,14
0,15
0,14
Regards,
Raymond