Rounding integers to nearest multiple of 10
Solution 1
I would just create a couple methods;
int RoundUp(int toRound)
{
if (toRound % 10 == 0) return toRound;
return (10 - toRound % 10) + toRound;
}
int RoundDown(int toRound)
{
return toRound - toRound % 10;
}
Modulus gives us the remainder, in the case of rounding up 10 - r
takes you to the nearest tenth, to round down you just subtract r. Pretty straight forward.
Solution 2
You don't need to use modulus (%) or floating point...
This works:
public static int RoundUp(int value)
{
return 10*((value + 9)/10);
}
public static int RoundDown(int value)
{
return 10*(value/10);
}
Solution 3
This code rounds to the nearest multiple of 10:
int RoundNum(int num)
{
int rem = num % 10;
return rem >= 5 ? (num - rem + 10) : (num - rem);
}
Very simple usage :
Console.WriteLine(RoundNum(143)); // prints 140
Console.WriteLine(RoundNum(193)); // prints 190
Solution 4
Divide the number by 10.
number = number / 10;
Math.Ceiling(number);//round up
Math.Round(number);//round down
Then multiply by 10.
number = number * 10;
Solution 5
A general method to round a number to a multiple of another number, rounding away from zero.
For integer
int RoundNum(int num, int step)
{
if (num >= 0)
return ((num + (step / 2)) / step) * step;
else
return ((num - (step / 2)) / step) * step;
}
For float
float RoundNum(float num, float step)
{
if (num >= 0)
return floor((num + step / 2) / step) * step;
else
return ceil((num - step / 2) / step) * step;
}
I know some parts might seem counter-intuitive or not very optimized. I tried casting (num + step / 2) to an int, but this gave wrong results for negative floats ((int) -12.0000 = -11
and such). Anyways these are a few cases I tested:
- any number rounded to step 1 should be itself
- -3 rounded to step 2 = -4
- -2 rounded to step 2 = -2
- 3 rounded to step 2 = 4
- 2 rounded to step 2 = 2
- -2.3 rounded to step 0.2 = -2.4
- -2.4 rounded to step 0.2 = -2.4
- 2.3 rounded to step 0.2 = 2.4
- 2.4 rounded to step 0.2 = 2.4
dhardy
Updated on January 17, 2020Comments
-
dhardy over 4 years
I am trying to figure out how to round prices - both ways. For example:
Round down 43 becomes 40 143 becomes 140 1433 becomes 1430 Round up 43 becomes 50 143 becomes 150 1433 becomes 1440
I have the situation where I have a price range of say:
£143 - £193
of which I want to show as:
£140 - £200
as it looks a lot cleaner
Any ideas on how I can achieve this?
-
Matthew Watson about 11 yearsGives the wrong answer for 1433 according to the spec.
-
Matthew Watson about 11 yearsGives the wrong answer for 1433 according to the spec
-
Joe about 11 yearsMight not give you what you want for very large numbers (e.g. Int32.MaxValue)
-
Matthew Watson about 11 yearsYes, RoundUp will fail for numbers higher than (int32.MaxValue-10). Don't think that's an issue for prices in pounds though. And it's not possible to round those numbers up anyway, by any means (unless you return a long).
-
DarkDust over 10 yearsThe
RoundUp
is broken. If you pass20
, it gets rounded up to30
which is most likely not what you want. You can either do a conditional test (if ((toRound % 10) == 0) return toRound;
) or use an unconditional rounding, for examplereturn ((toRound + 9) / 10) * 10;
-
Adam Knights over 10 yearsNote that the logic above only works for positive numbers (as I just found out).
-
Adam Knights over 10 yearsChange to
Math.Abs(toRound) % 10
if you have negatives anywhere in price calc or otherwise. -
user247702 almost 9 yearsAs stated in the first comment on your answer,
RoundUp(20)
returns30
. -
fiorix over 8 yearsThis worked well for me, and I like that it does not use modulus. Equivalent in Go: func RoundUp(v int) int { return 10 * ((v + 9) / 10) } func RoundDown(v int) int { return 10 * (v / 10) }
-
Kurt Van den Branden over 5 yearsWhile this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.