Skip to Content
author's profile photo Former Member
Former Member

dividing 1/3 problem

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.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Posted on Aug 17, 2016 at 07: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


    img_124.png (2.2 kB)
    img_123.png (2.3 kB)
    Add a comment
    10|10000 characters needed characters exceeded

    • 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

  • Posted on Aug 17, 2016 at 04: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.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.