Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a name or built in c# function for this type of division [duplicate]

Tags:

c#

math

I have this extension method, but I don't know whether there is already a built in operator for it or if there is a name for it:

public static class IntegerExtensions
{
    public static int DivideWholeAndPartial(this int total, int divisor)
    {
        return (total / divisor) +
               ((total % divisor) == 0 ? 0 : 1);
    }
}

Essentially if the total divides equally, then that is returned. If there are any remainders it is rounded up - i.e. include partials as a whole. Kind of like a "How many One litre bottles are needed to hold 3.5 litres of water" problem.

Am I missing any built in C# methods? Is there a name for this?

like image 260
oatsoda Avatar asked Mar 07 '14 15:03

oatsoda


1 Answers

I think Ceiling does what you want:

return (int)Math.Ceiling((double)total / (double)divisor);

.. but I think you'll have to keep your extension method, as there isn't a built-in method that does this.

like image 178
Chris Mantle Avatar answered Sep 28 '22 00:09

Chris Mantle