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.
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
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.
Add a comment