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

doubleroundToNearestMultiple(double number, double multiple)
round To Nearest Multiple
return (int) (number / multiple) * multiple;
longroundToNearestMultipleOf(final long num, final long multipleOf)
round To Nearest Multiple Of
if (num < 0) {
    throw new IllegalArgumentException("num cannot be negative");
if (multipleOf < 1) {
    throw new IllegalArgumentException("cannot round to nearest multiple of values less than 1");
if (num < multipleOf) {
    return multipleOf;
...
floatroundToNearestN(float value, float n)
round To Nearest N
return n * (Math.round(value / n));
doubleroundToNearestPowerOfTen(double value)
Round down input value to nearest value of 10.
return Math.pow(10, Math.floor(log10(value)));
introundToNext(int val, int factor)
round To Next
int pval = Math.max(val, factor);
return ((pval + factor - 1) / factor) * factor;
introundToNextPowerOfTwo(int value)
Returns the closest power of two for the given value.
if (value < 0) {
    throw new UnsupportedOperationException();
value--;
value |= value >>> 1;
value |= value >>> 2;
value |= value >>> 4;
value |= value >>> 8;
...
doubleroundToNthDecimal(double number, int decimals)
round To Nth Decimal
return roundToNearest(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
doubleroundToNumDigits(double d, int n)
Rounds the input double to n significant digits.
if (d == 0) {
    return 0;
final long numDs = mostSignificantDigitPosition(d);
final long power = n - ((numDs > 0) ? numDs : (numDs + 1));
final double mag = Math.pow(10, power);
final long shifted = Math.round(d * mag);
return shifted / mag;
...
introundToPowerOf(int i, int powerOf)
round To Power Of
int j = 0;
while (true) {
    j++;
    int k = pow(powerOf, j);
    if (k >= i)
        return k;
doubleroundToPowerOf10(double value, int powerOf10)
round To Power Of
if (Double.isInfinite(value) || Double.isNaN(value)) {
    return value;
value *= Math.pow(10, -powerOf10);
value = Math.round(value);
value /= Math.pow(10, -powerOf10);
return value;