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 v)
Rounds double to the closest integer value.
return Math.floor(v + 0.5);
doubleround(double v, int precision)
Fast way of truncating a double to a certain number of digits.
assert precision >= 0 && precision < TENS.length;
double unscaled = v * TENS[precision];
if (unscaled < Long.MIN_VALUE || unscaled > Long.MAX_VALUE)
    return v;
long unscaledLong = (long) (unscaled + (v < 0 ? -0.5 : 0.5));
return (double) unscaledLong / TENS[precision];
doubleround(double val)
round
int precision = 10000; 
return Math.floor(val * precision + .5) / precision;
longround(double val)
Our own implementation of Math.round due to difference between java 1.6 and 1.7 implementations See Java bug 6430675 Using java 1.6 implementation of Math.round defined as floor of 0.5d plus value.
return (long) Math.floor(val + 0.5d);
intround(double val)
round
return (int) Math.round(val);
doubleround(double val, int decimalHouses)
round
double base = Math.pow(10d, decimalHouses);
double result = Math.round(val * base) / base;
return result;
doubleround(double val, int decimalPlaces)
Rounds a given double to any number of decimal places.
val = val * (Math.pow(10, (double) decimalPlaces));
long intval = Math.round(val);
val = (double) intval;
val = val / (Math.pow(10, (double) decimalPlaces));
return val;
doubleround(double val, int places)
AUTHOR : Ravi Kiran DATE : 18-08-06 NAME : round DESCRIPTION : Round a double value to a specified number of decimal places ARGUMENTS : val - the value to be rounded places - the number of decimal places to round to RETURNS : val - rounded to places decimal places NOTES : None CHANGE HISTROY Author Date Description
long factor = 0;
long tmp = 0;
try {
    factor = (long) Math.pow(10, places);
    val = val * factor;
    tmp = Math.round(val);
} catch (Exception exception) {
    System.err.println("JAVA UI Interface: Exception occured in LipiTKUtil while triming float value :"
...
doubleround(double val, int places)
round
long factor = (long) Math.pow(10, places);
val = val * factor;
long tmp = Math.round(val);
return (double) tmp / factor;
doubleround(double val, int places)
Round value.
if (places < 0)
    throw new IllegalArgumentException();
long factor = (long) Math.pow(10, places);
val *= factor;
long tmp = Math.round(val);
return (double) tmp / factor;