Built in .Net algorithm to round value to the nearest 10 interval
How to, in C# round 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.
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
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.
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. =)
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.
Old question but here is a way to do what has been asked plus I extended it to be able to round any number to the number of sig figs you want.
private double Rounding(double d, int digits)
{
int neg = 1;
if (d < 0)
{
d = d * (-1);
neg = -1;
}
int n = 0;
if (d > 1)
{
while (d > 1)
{
d = d / 10;
n++;
}
d = Math.Round(d * Math.Pow(10, digits));
d = d * Math.Pow(10, n - digits);
}
else
{
while (d < 0.1)
{
d = d * 10;
n++;
}
d = Math.Round(d * Math.Pow(10, digits));
d = d / Math.Pow(10, n + digits);
}
return d*neg;
}
private void testing()
{
double a = Rounding(1230435.34553,3);
double b = Rounding(0.004567023523,4);
double c = Rounding(-89032.5325,2);
double d = Rounding(-0.123409,4);
double e = Rounding(0.503522,1);
Console.Write(a.ToString() + "\n" + b.ToString() + "\n" +
c.ToString() + "\n" + d.ToString() + "\n" + e.ToString() + "\n");
}