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

floatroundPower10(float val, int powerOf10)
Round a float to a particular power of 10
double precision = Math.pow(10, powerOf10);
double mantisa = Math.floor((val / precision) + 0.5);
float value = (float) (mantisa * precision);
return value;
longroundPower2(final long x)
Finds greater nearest number that is power of 2
int rval = 256;
while (rval < x)
    rval <<= 1;
return rval;
doubleroundRadian(double rad)
Round radian value in range -pi/2 to pi/2
final double pi2 = Math.PI * 2;
if (rad > 0) {
    return rad - pi2 * (int) ((rad + Math.PI) / pi2);
} else {
    return rad + pi2 * (int) ((-rad + Math.PI) / pi2);
introundRobin(final int value)
round Robin
int roundRobin = 0;
for (int i = 1; i < value; i++) {
    roundRobin += i;
return roundRobin;
doubleroundScale(double value, int scale)
round Scale
double d = Math.pow(10, scale);
return Math.round(value * d) / d;
DoubleroundScale2(Number number)
rounds the number to a value with 2 decimal digits {Category} NumberUtil {talendTypes} double | Double {param} double(2.5) number: number to format {example} numberToString(1234.12345) result: 1234.12
if (number != null) {
    return Math.round(number.doubleValue() * 100d) / 100d;
} else {
    return null;
introundSec(int sec)
round Sec
for (int i = 0; i * STEP < 60; i++) {
    if (sec >= i && sec < STEP * (i + 1)) {
        sec = i * STEP;
        break;
return sec;
longroundSec(long milli)
round Sec
return ((milli + 500L) / 1000L) * 1000L;
doubleroundSig(double d, int n)
round Sig
if (d != d) {
    return Double.NaN;
if (d == 0) {
    return 0;
int power = n - (int) Math.ceil(Math.log10(d < 0 ? -d : d));
double magnitude = Math.pow(10, power);
...
doubleroundSignificant(double d, int numDigit, boolean keepInteger)
Round specified value to specified number of significant digit.
If keepInteger is true then integer part of number is entirely conserved.
final double digit = Math.ceil(Math.log10(Math.abs(d)));
if ((digit >= numDigit) && keepInteger)
    return Math.round(d);
final double pow = Math.pow(10, numDigit - digit);
return Math.round(d * pow) / pow;