List of utility methods to do Number Round
double | roundToNearestMultiple(double number, double multiple) round To Nearest Multiple return (int) (number / multiple) * multiple; |
long | roundToNearestMultipleOf(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; ... |
float | roundToNearestN(float value, float n) round To Nearest N return n * (Math.round(value / n));
|
double | roundToNearestPowerOfTen(double value) Round down input value to nearest value of 10. return Math.pow(10, Math.floor(log10(value)));
|
int | roundToNext(int val, int factor) round To Next int pval = Math.max(val, factor); return ((pval + factor - 1) / factor) * factor; |
int | roundToNextPowerOfTwo(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; ... |
double | roundToNthDecimal(double number, int decimals) round To Nth Decimal return roundToNearest(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
|
double | roundToNumDigits(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; ... |
int | roundToPowerOf(int i, int powerOf) round To Power Of int j = 0; while (true) { j++; int k = pow(powerOf, j); if (k >= i) return k; |
double | roundToPowerOf10(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; |