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

introundToMultipleXLength(int inLength, int multipler)
round To Multiple X Length
if (multipler == 0)
    return inLength;
return inLength % multipler == 0 ? inLength : inLength + multipler - inLength % multipler;
doubleroundToN(double d, int n)
Rounds the d to n decimal places.
double places = StrictMath.pow(10, n);
double p = StrictMath.round(d * places) / places;
return p;
doubleroundToNDecimalPlaces(final double in, final int n)
Rounds the double to the given number of decimal places.
if (n < 1) {
    throw new IllegalArgumentException("cannot round to " + n + " decimal places");
final double mult = Math.pow(10, n);
return Math.round((in + Math.ulp(in)) * mult) / mult;
doubleroundToNDigits(double d, int n)
round To N Digits
if (d == 0)
    return d;
int log = (int) Math.log10(d);
int exp = n;
exp -= log;
int ival = (int) (Math.round(d * Math.pow(10, exp)));
return ival / Math.pow(10, exp);
doubleroundToNearest(double d)
round To Nearest
return Math.floor(d + 0.5);
doubleroundToNearest(double number)
round To Nearest
if ((int) (number + .5) >= (int) (number))
    return (int) number + 1;
return (int) number;
introundToNearest(int num, int nearest)
Rounds the number to the nearest.
return (int) (nearest * Math.round((double) num / (double) nearest));
introundToNearestEven(int mantissa, int numOfBitsToRoundOff)
Rounds the mantissa with bitsRoundOff
assert (numOfBitsToRoundOff < 32);
int a = 1 << (numOfBitsToRoundOff - 1) - 1;
int b = (mantissa >> numOfBitsToRoundOff) & 1;
return (mantissa + a + b) >> numOfBitsToRoundOff;
introundToNearestInterval(int i, int j)
round To Nearest Interval
return Math.round(i / j) * j;
introundToNearestK(float v, int k)
round To Nearest K
return (int) (Math.round(v / k) * k);