11

I am trying to divide a number into multiple parts so the sum of the part are equal to the input number.

If I have 3.99 and if I need to divide into two parts, the expected output is 2 and 1.99 (2+1.99=3.99)

And if I need to divide 3.99 into 3 parts, the expected output is 1.3, 1.3 and 1.39 (1.3+1.3+1.39=3.99)

I was able to output 2.99 and 1 for the first example and I was able to get 1.99, 1 and 1 for the second example. However, in both the cases results are very far from the expected output.

I need to split postal charges into separate line items. The result could be equally divided or unequally divided while making sure that the sum of the different parts is equal to the input number.

Here are some other examples:

3.99 divided in 3 parts: 1.33, 1.33, 1.33
3.98 divided in 3 parts: 1.33, 1.33, 1.32
3.97 divided in 3 parts: 1.32, 1.32, 1.33
3.96 divided in 3 parts: 1.32, 1.32, 1.32
3.95 divided in 3 parts: 1.32, 1.32, 1.31
15
  • 10
    Why wouldn't 3.99 divided into 3 parts be 1.33, 1.33, 1.33 instead?
    – Tyler Lee
    Nov 27, 2012 at 13:47
  • 1
    "But in both the cases result is very far from the expected output" -- how did it vary? What did you try? What was the expected outcome? What Have You tried?
    – hometoast
    Nov 27, 2012 at 13:49
  • 3
    @Naresh Why not 1.32, 1.32, 1.33?
    – Rawling
    Nov 27, 2012 at 13:52
  • 2
    I think this is a perfectly good question, it just wasn't expressed very clearly. I think what Naresh wants to do is similar to splitting the bill at a restaurant. You want to split the value evenly, but cannot charge a value less than 0.01 -- so in some cases, one person gets charged 0.01 more/less than everyone else.
    – Jon B
    Nov 27, 2012 at 14:09
  • 1
    @Naresh You examples are much clearer now, but your 3.95 and 3.98 examples are inconsistent - for one you have x, x, x+0.02 and for the other you have x, x+0.01, x+0.01. Is there a reason these are treated differently e.g. you prefer "even" numbers?
    – Rawling
    Nov 27, 2012 at 14:30

3 Answers 3

6

I tested with your supplied values. May need some tweaking:

static IEnumerable<decimal> SplitValue2(decimal value, int count)
{
    if (count <= 0) throw new ArgumentException("count must be greater than zero.", "count");
    var result = new decimal[count];

    decimal runningTotal = 0M;
    for (int i = 0; i < count; i++)
    {
        var remainder = value - runningTotal;
        var share = remainder > 0M ? Math.Max(Math.Round(remainder / ((decimal)(count - i)), 2), .01M) : 0M;
        result[i] = share;
        runningTotal += share;
    }

    if (runningTotal < value) result[count - 1] += value - runningTotal;

    return result;
}

This assumes you pass in a value with 2 decimal precision. If you pass in 3.999, you're not going to get correct results.

8
  • 1
    If you want you can use (decimal.GetBits(value)[3] >> 16) % 256 to get the number of decimal places specified in the input decimal.
    – Rawling
    Nov 27, 2012 at 14:07
  • 1
    This doesn't work for count > 3; e.g. 0.03 split between 5 gives .01 .01 .01 .01 -.01 not .01 .01 .01 0 0.
    – Rawling
    Nov 27, 2012 at 14:50
  • @Rawling good catch. It's a bit of an edge case, but I can fix it.
    – Jon B
    Nov 27, 2012 at 14:57
  • Do it quick, because I'm posting my own version if this gets reopened :p
    – Rawling
    Nov 27, 2012 at 14:58
  • 1
    Maybe the negative part was a poor example; this still saves all the "difference" for the last number, e.g. (1.03M, 5) gives .21 .21 .21 .21 .19 rather than .21 .21 .21 .20 .20.
    – Rawling
    Nov 27, 2012 at 15:08
4

In case someone is intending to split in rounded numbers using an extension method, It ca be done so by doing the below. Eg. split 25 into 4 would give a result like 6,6,6,7. Thanks @ Jon B

 public static IEnumerable<int> PartitionMeInto(this int value, int count)
    {
        if (count <= 0) throw new ArgumentException("count must be greater than zero.", "count");
        var result = new int[count];

        int runningTotal = 0;
        for (int i = 0; i < count; i++)
        {
            var remainder = value - runningTotal;
            var share = remainder > 0 ? remainder / (count - i) : 0;
            result[i] = share;
            runningTotal += share;
        }

        if (runningTotal < value) result[count - 1] += value - runningTotal;

        return result;
    }

Usage

        int value = 25;

        var result = value.PartitionMeInto(4);
3

I had a requirement in which a number for ex: 29 is divided into a constant arrays and the remainder has to be appended at the last.

29= 9,9,9,3. for this my code is.

       List<int> split(int num,int splitBy)
           {

               List<int> r = new List<int>();
               int v = Convert.ToInt32(num / splitBy);
               r.AddRange(Enumerable.Repeat(splitBy, v).ToArray());
               var remaining = num % splitBy;
               if (remaining != 0)
                   r.Add(remaining);
               return r;

           }

Happy coding!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.