Java Utililty Methods Number Round

List of utility methods to do Number Round

Description

The list of methods to do Number Round are organized into topic(s).

Method

doubleRoundHalfUp(double d)
Round Half Up
return .5 * Math.ceil(d / .5);
doubleroundIfClose(double val)
Local function to round "sufficiently close" Number values to whole numbers, to prevent unnecessary aliasing in drawing shapes.
double nVal = Math.round(val);
if (Math.abs(val - nVal) < 0.001) {
    return nVal;
} else {
    return val;
StringRounding(double d, int i)
Rounding
long a = (long) d;
long b = (long) ((d - a) * Math.pow(10, i));
return a + "." + b;
doublerounding(double val)
rounding
long value = (long) val;
long decVal = 1;
for (long x = value; x >= 10; x /= 10) {
    decVal *= 10;
if (value > decVal * 5) {
    return decVal * 10;
} else if (value > decVal * 2) {
...
introundingRightShift(int x, int count)
Right-shift x by count bits, and round the result using half-even rounding.
int remainder;
if (count > 32) {
    return 0;
} else if (count == 32) {
    remainder = x;
    x = 0;
} else {
    remainder = x << (32 - count);
...
introundIntegerToNearestUpperTenth(int a)
round Integer To Nearest Upper Tenth
int remainder = a % 10;
while (remainder != 0) {
    a += 1;
    remainder = a % 10;
return a;
introundInventorySize(final int size)
round Inventory Size
if (size > 9 && size <= 18) {
    return 18;
} else if (size > 18 && size <= 27) {
    return 27;
} else if (size > 27 && size <= 36) {
    return 36;
} else if (size > 36 && size <= 45) {
    return 45;
...
StringroundJs(double v, int n)
round Js
return String.valueOf(v);
longroundLong(double a)
Returns a double rounded to the nearest integer.
return (long) Math.floor(a + 0.5);
introundM(int n, int m)
round M
return (int) (Math.floor((n + m - 1) / m) * m);