Java Utililty Methods Double Number Round

List of utility methods to do Double Number Round

Description

The list of methods to do Double Number Round are organized into topic(s).

Method

doubleround(double value, double increment)
round
return Math.round(value / increment) * increment;
doubleround(double value, double precision)
rounding with decimal precision
return Math.round(value / precision) * precision;
doubleround(double value, int decimal)
round
long d = 1;
int c = decimal < 0 ? -decimal : decimal;
for (int i = 0; i < c; i++) {
    d = d * 10;
if (decimal >= 0) {
    return (double) Math.round(value * d) / d;
return Math.round(value / d) * d;
doubleround(double value, int decimal)
Rounds the passed value to the specified number of decimals.
if (decimal <= 0)
    return value;
double p = Math.pow(10, decimal);
value = value * p;
return Math.round(value) / p;
doubleround(double value, int decimalPlace)
round
double power_of_ten = 1;
while (decimalPlace-- > 0)
    power_of_ten *= 10.0;
return Math.round(value * power_of_ten) / power_of_ten;
doubleround(double value, int decimalPlaces)
Rounds the value with the specified number of decimal places.
if (decimalPlaces < 0) {
    throw new IllegalArgumentException("Decimal places has to be non negative.");
} else if (decimalPlaces == 0) {
    return Math.round(value);
} else {
    final double factor = Math.pow(10, decimalPlaces);
    return Math.round(value * factor) / factor;
doubleround(double value, int decimalPlaces)
round
double factor = Math.pow(10.0, decimalPlaces);
long longValue = Math.round(value * factor);
double result = longValue / factor;
return result;
doubleround(double value, int decimals)
Rounds given value to requested number of decimals
if (decimals < 0) {
    throw new RuntimeException("Decimals must be positive or zero");
double powerOfTen = Math.pow(10.0, decimals);
double multipliedByPower = value * powerOfTen;
long rounded = Math.round(multipliedByPower);
double divided = (double) rounded / powerOfTen;
return divided;
...
doubleround(double value, int decimals)
round
int base = (int) Math.pow(10, decimals);
return Math.floor(value * base) / base;
doubleround(double value, int exponent)
Round the value to the specified exponent
double factor = Math.pow(10, exponent);
return Math.round(value * factor) / factor;