Rounding Integers to Nearest Multiple of 10

Rounding integers to nearest multiple of 10

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.

How to round an integer up or down to the nearest 10 using Javascript

Divide the number by 10, round the result and multiply it with 10 again, for example:

  1. 33 / 10 = 3.3
  2. 3.3 rounded = 3
  3. 3 × 10 = 30

console.log(Math.round(prompt('Enter a number', 33) / 10) * 10);

Rounding up to the nearest multiple of a number

This works for positive numbers, not sure about negative. It only uses integer math.

int roundUp(int numToRound, int multiple)
{
if (multiple == 0)
return numToRound;

int remainder = numToRound % multiple;
if (remainder == 0)
return numToRound;

return numToRound + multiple - remainder;
}

Edit: Here's a version that works with negative numbers, if by "up" you mean a result that's always >= the input.

int roundUp(int numToRound, int multiple)
{
if (multiple == 0)
return numToRound;

int remainder = abs(numToRound) % multiple;
if (remainder == 0)
return numToRound;

if (numToRound < 0)
return -(abs(numToRound) - remainder);
else
return numToRound + multiple - remainder;
}

Ruby 2.0 - Rounding an integer to the nearest multiple of 10

Integer#round has the functionality.

You pass a negative number to round to represent which 10's digit you'd want to round to. For example:

Round to the nearest 10:

55.round(-1) # => 60

To round to the nearest 100:

550.round(-2) # => 600

how to find nearest equal or higher multiple of ten in python

You can use round(x / 10.0) * 10 instead of math.ceil.

Even easier would be

def custom_round(x):
return int(round(x, -1))

This immediately rounds to a multiple of ten.

Rounding pos and neg numbers to the nearest 10

What about this:

return value > 0 ? 
Math.Ceiling(value / 10) * 10 :
Math.Ceiling(Math.Abs(value) / 10) * -10;

Rounding down integers to nearest multiple

def round_down(num, divisor):
return num - (num%divisor)

In [2]: round_down(19,10)
Out[2]: 10

In [3]: round_down(19,5)
Out[3]: 15

In [4]: round_down(10,10)
Out[4]: 10

Rounding numbers to nearest multiple of 5 or 10 in the same range depending on what's first

public static int GetLimit(int v)
{
int by5 = v / 5;

// for too small numbers we return early
if (by5 < 0) return 0;
if (by5 < 5) return 5;
if (by5 < 10) return 10;

int log = (int)Math.Log10(by5) - 1;
int scope = (int)Math.Pow(10, log);
int tmp = by5 / scope;
tmp = 5 * ((tmp/5) + 1);
return tmp * scope;
}

This uses the decimal logarithm to get the "range" (scope). Then it divides the number by that scope to get the first two digits, "rounds" them to the next multiple of 5 (5*((tmp/5) + 1)) and multiplies by the scope again to get back to the range.

For too small numbers (below 50) it returns early, because scope would become 0 which would lead to an DivideByZeroException.

Console.WriteLine(GetLimit(685425));
Console.WriteLine(GetLimit(53));
Console.WriteLine(GetLimit(8215));
Console.WriteLine(GetLimit(11));
Console.WriteLine(GetLimit(2));

results in

150000
15
2000
5
5

An alternative to using Math.Log and Math.Pow would be to convert the divided number to a string work with thefirst two digits and the length. I did not test if this could be faster, but I don't think so. If this is performance critical you'd need to test it.



Related Topics



Leave a reply



Submit