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

introundUpToPowerOfTwo(int v)
round Up To Power Of Two
if (v < 1)
    return 1;
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
...
introundUpToPowerOfTwo(int x)
Round the given number to the next power of two
x = x - 1;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x + 1;
introundUpToTheNextHighestPowerOf2(int v)
Compute the power of 2 which produces an number containing some particular value.
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
...
DoubleroundUpToTwo(Double number)
round Up To Two
return Math.floor(number * 100) / 100.00;
doubleroundValue(Double value)
Rounds to 2 significant figures.
if (value.isInfinite() || value.isNaN() || value == 0)
    return value;
double val = value;
int shift = 0;
if (Math.abs(val) >= 1) 
    val *= 10;
    while (Math.abs(val) > 100) {
...
doubleroundValue(final double value)
Round up a double value
return roundValue(value, 0.00000000000001);
double[]roundValues(Double value1, Double value2)
Rounds to 2 significant figures, relatively.
if (value1.isInfinite() || value1.isNaN() || value1 == 0 || value2.isInfinite() || value2.isNaN()
        || value2 == 0)
    return new double[] { roundValue(value1), roundValue(value2) };
double val1 = value1;
double val2 = value2;
double biggerValue;
if (Math.abs(val1) >= Math.abs(val2))
    biggerValue = val1;
...
doubleroundValueToUnit(final double graphValue, final double graphUnit, final boolean isMinValue)
Round floating value by removing the trailing part, which causes problem when creating units.
if (graphUnit < 1) {
    if (graphValue < 0) {
        final double gvDiv1 = graphValue / graphUnit;
        final int gvDiv2 = (int) (gvDiv1 - 0.5f);
        final double gvDiv3 = gvDiv2 * graphUnit;
        return gvDiv3;
    } else {
        final double gvDiv1 = graphValue / graphUnit;
...
doubleroundy(double a)
roundy
return roundy(a, precisionFor(a));
doubleroundy(double a, double precision)
Returns the closest double to the argument that is equal for at least a precision value.
return (double) Math.round(a / precision) * precision;