tags:

views:

5382

answers:

4

How to, in C# round up any value to 10 interval? For example, if I have 11, I want it to return 10, if I have 136, then I want it to return 140.

I can easily do it by hand

return ((int)( number/10))*10

But I am looking for an builtin algorithm to do this job, something like Math.Round(). The reason why I won't want to do by hand is that I don't want to write same or similar piece of code all over my projects, even for something as simple as the above.

+2  A: 

Rounding a float to an integer is similar to (int)(x+0.5), as opposed to simply casting x - if you want a multiple of 10, you can easily adapt that.

If you just want to do integer math and are rounding it to ten, try (x+10/2)/10*10.

Edit: I noticed that this response doesn't meet the original's author's request, and is also a biased form of rounding that I prefer not to do. However, another accepted response already stated Math.round(), a much better solution.

Raymond Martineau
I have to say that, even though this is the trick I used in past a lot, Math.Round looks a lot cleaner.
Dave Van den Eynde
A: 

Use Math.Ceiling to always round up.

int number = 236;
number = (int)(Math.Ceiling(number / 10.0d) * 10);

Modulus(%) gets the remainder, so you get:

// number = 236 + 10 - 6

Put that into an extension method

public static int roundupbyten(this int i){
    // return i + (10 - i % 10); <-- logic error. Oops!
    return (int)(Math.Ceiling(i / 10.0d)*10); // fixed
}

// call like so:
int number = 236.roundupbyten();

above edited: I should've gone with my first instinct to use Math.Ceiling

I blogged about this when calculating UPC check digits.

Atømix
Try it with 240. What do you get then? Or for that matter, 11.
Chris Charabaruk
for the archives: edited this post @ 7:30 the same day
Atømix
+12  A: 

There is no built-in function in the class library that will do this. The closest is System.Math.Round() which is only for rounding numbers of types Decimal and Double to the nearest integer value. However, you can wrap your statement up in a extension method, if you are working with .NET 3.5, which will allow you to use the function much more cleanly.

public static class ExtensionMethods
{
    public static int RoundOff (this int i)
    {
        return ((int)Math.Round(i / 10.0)) * 10;
    }
}

int roundedNumber = 236.RoundOff(); // returns 240
int roundedNumber2 = 11.RoundOff(); // returns 10

If you are programming against an older version of the .NET framework, just remove the "this" from the RoundOff function, and call the function like so:

int roundedNumber = ExtensionMethods.RoundOff(236); // returns 240
int roundedNumber2 = ExtensionMethods.RoundOff(11); // returns 10
Chris Charabaruk
There's a bunch of problems with this code, namely it doesn't compile (missing return type) and it doesn't round to the nearest. In your example, it returns 230 and 10.
Dave Van den Eynde
Fixed, try it now.
Chris Charabaruk
It still won't return 240, because you're rounding down.
Dave Van den Eynde
Actually run it. Here's the source (including a test class), and even a build of the source. It works, and correctly. http://labs.coldacid.net/code/number-rounding-extension-method
Chris Charabaruk
Also, read the docs for Math.Round().
Chris Charabaruk
Ah, but you're original answer didn't use Math.Round() at all.
Dave Van den Eynde
Also, you changed 10 into 10.0, which is useful to know, otherwise it won't compile.
Dave Van den Eynde
I said I fixed it, didn't I?
Chris Charabaruk
+2  A: 

This might be a little too late but I guess this might be of good help someday...

I have tried this:

public int RoundOff(int number, int interval){
    int remainder = number % interval;
    number += (remainder < interval / 2) ? -remainder : (interval - remainder);
    return number;
}

To use:

int number = 11;
int roundednumber = RoundOff(number, 10);

This way, you have the option whether if the half of the interval will be rounded up or rounded down. =)

Jronny