2

My scenario is that if

47/15= 3.13333

i want to convert it into 4, if the result has decimal i want to increase the result by 1, right now i am doing this like

        float res = ((float)(62-15) / 15);
        if (res.ToString().Contains("."))
        {
             string digit=res.ToString().Substring(0, res.ToString().IndexOf('.'));
             int incrementDigit=Convert.ToInt16(k) + 1;


        }

I want to know is there any shortcut way or built in function in c# so that i can do this fast without implementing string functions.

Thanks a lot.

Mogli
  • 1,972
  • 11
  • 34
  • 67
  • 4
    Math.Ceiling?: http://msdn.microsoft.com/en-us/library/vstudio/zx4t0t48.aspx – Fox32 May 10 '13 at 12:20
  • http://stackoverflow.com/questions/904910/how-do-i-round-a-float-up-to-the-nearest-int-in-c – Steve May 10 '13 at 12:21
  • 1
    Do you mean something like the `System.Math.Ceiling` which rounds up? (Subquestion: if your number is negative `-3.13333` should that still "add" one and make -4 or _really_ add and make it -3?) – Chris Sinclair May 10 '13 at 12:21
  • Did you check [`Math.Ceiling`](http://stackoverflow.com/questions/8832978/how-to-convert-math-ceiling-result-to-int) ? – V4Vendetta May 10 '13 at 12:21
  • @ChrisSinclair No, My number will number will never have negative value. – Mogli May 10 '13 at 12:22

5 Answers5

9

Do you mean you want to perform integer division, but always rounding up? I suspect you want:

public static int DivideByFifteenRoundingUp(int value) {
    return (value + 14) / 15;
}

This avoids using floating point arithmetic at all - it just allows any value which isn't an exact multiple of 15 to be rounded up, due to the way that integer arithmetic truncates towards zero.

Note that this does not work for negative input - for example, if you passed in -15 this would return 0. you could fix this with:

public static int DivideByFifteenRoundingUp(int value) {
    return value < 0 ? value / 15 : (value + 14) / 15;
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
5

Use Math.Ceiling Quoting MSDN:

Returns the smallest integral value that is greater than or equal to the specified decimal number.

Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
  • There's no need to do this though - there are easy ways of always rounding up, without going to floating point at all. – Jon Skeet May 10 '13 at 12:22
  • 1
    You are right, but assuming he has result 3.13333, he have floating point. Your code is faster but much difficult to understand for a beginner. – Piotr Stapp May 10 '13 at 12:24
  • 1
    But if you take a step back, he doesn't have that result - he just wants integer division, rounding up. Note that in your version you then need to cast back to `int` *and* it wil give "interesting" results for very large numbers, where `float`'s granularity is larger than an integer. – Jon Skeet May 10 '13 at 12:36
4

You are looking for Math.Ceiling().

Convert the value you have to a Decimal or Double and the result of that method is what you need. Like:

double number = ((double)(62-15) / (double)15);
double result = Math.Ceiling(number);

Note the fact that I cast 15 to a double, so I avoid integer division. That is most likely not what you want here.

pyrocumulus
  • 9,072
  • 2
  • 43
  • 53
0

Another way of doing what you ask is to add 0.5 to every number, then floor it (truncate the decimal places). I'm afraid I don't have access to a C# compiler right now to confirm the exact function calls!

NB: But as others have confirmed, I would think the Math.Ceiling function best communicates to others what you intend.

Nij
  • 2,028
  • 2
  • 22
  • 27
0

Something like:

float res = ((float)(62-15) / 15);

int incrementDigit = (int)Math.Ceiling(res);

or

int incrementDigit  = (int)(res  + 0.5f);
Toto
  • 89,455
  • 62
  • 89
  • 125
TelKitty
  • 3,146
  • 3
  • 18
  • 21