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, int nbDigits)
round
double val = value;
if (nbDigits > 0) {
    double p = Math.pow(10, nbDigits);
    val = Math.round(value * p) / p;
return val;
doubleround(double value, int numberOfDecimalPlaces)
round
double multipicationFactor = Math.pow(10, numberOfDecimalPlaces);
return Math.round(value * multipicationFactor) / multipicationFactor;
doubleround(double value, int numDecimals)
Round a value to the specified number of decimal places using the "Round Half Up" strategy.
double scale = (numDecimals < 8) ? SCALE[numDecimals] : Math.pow(10, numDecimals);
if (Math.ulp(value) * scale > 1d)
    return value;
return Math.floor(value * scale + 0.5) / scale;
doubleround(double value, int places)
round
if (places < 0)
    throw new IllegalArgumentException();
long factor = (long) Math.pow(10, places);
return (double) Math.round(value * factor) / factor;
doubleround(double value, int places)
Round the given double value to the specified number of places after the decimal point.
if (places < 0)
    throw new IllegalArgumentException();
long factor = (long) Math.pow(10, places);
value = value * factor;
long tmp = Math.round(value);
return (double) tmp / factor;
doubleround(double value, int power)
Relaxes precision up to the given power of 2.
double scaled = Math.scalb(value, -power);
return Math.scalb(Math.rint(scaled), power);
doubleround(double value, int precision)
round
double pow = Math.pow(10d, precision);
double mulVal = pow * value;
double kept = Math.floor(mulVal + 0.5d);
return kept / pow;
doubleround(double value, int precision)
Given a double value, return a new double with precision as given.
double newValue = Math.round(value * Math.pow(10, precision)) / Math.pow(10, precision);
return newValue;
doubleround(double value, int precision)
Round a double up or down by the given number of decimal places
double factor = Math.pow(10, precision);
double result = Math.round(value * factor) / factor;
return result;
doubleround(double value, int roundingFactor)
round
double doubleRounded = Math.round(value * roundingFactor);
return doubleRounded / roundingFactor;