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:
33 / 10 = 3.3
3.3 rounded = 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
Why Is Infinity Printed as "8" in the Windows 10 Console
Custom JSON Serialization for Each Item in Ienumerable
Deserialize Specific Enum into System.Enum in JSON.Net
How to Send an Email in .Net According to New Security Policies
How to Get a List<String> Collection of Values from App.Config in Wpf
How to Write Super-Fast File-Streaming Code in C#
How to Open a Serial Port by Friendly Name
C# Quickest Way to Shift Array
Xml Parsing - Read a Simple Xml File and Retrieve Values
MVC 4 Edit Modal Form Using Bootstrap
Retrieve the Respective Coordinates of All Words on the Page with Itextsharp
When to Use Properties Instead of Functions
Linq to SQL Where Clause Optional Criteria
Xunit.Net: Global Setup + Teardown
Possible to Access MVC Viewbag Object from JavaScript File