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

dividing 1/3 problem

Former Member
0 Likes
2,519

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.

6 REPLIES 6
Read only

Sandra_Rossi
Active Contributor
0 Likes
2,135

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.

Read only

0 Likes
2,135

This message was moderated.

Read only

Flavio
Active Contributor
0 Likes
2,135

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

Read only

Former Member
0 Likes
2,135

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.

Read only

matt
Active Contributor
0 Likes
2,135

Highly commendable. If you find some standard functionality, please post it. I've never found any!

Read only

0 Likes
2,135

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