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

StringgetRatioString(double n, double d)
get Ratio String
if (d < 0.0001) {
    return "----";
DecimalFormat format = new DecimalFormat("#0.0%");
return format.format(n / d);
doubleround(double a)
Returns a double rounded to the nearest integer.
return Math.floor(a + 0.5);
intRound(double a)
Floor if Value < 0.5, Ceil if Value >= 0.5
int tmp = ((int) (a * 100)) % 100;
if (tmp < 50) {
    return Floor(a);
} else {
    return Ceil(a);
intround(double a)
round
return (int) (((a + 0.5) * 10.0) / 10.0);
doubleround(double a, double precision)
Mathematically rounds a number with a defined precision.
if (precision == 0.0) {
    return 0.0;
return Math.round(a / precision) * precision;
doubleround(double a, int cutOfDigits)
Modification of Math#round(double) taking an additional argument representing the requested accuracy in the following way:
double fac = Math.pow(10, digits);
return fac * Math.round(a / fac);
double fac = Math.pow(10, cutOfDigits);
return fac * Math.round(a / fac);
doubleround(double a, int decimal)
round
try {
    String str = String.valueOf(a);
    int idx = str.indexOf(".");
    if (idx == -1 || idx == str.length() - 1)
        return Double.parseDouble(str);
    return Double.parseDouble(str.substring(0, Math.min(str.length(), idx + decimal + 1)));
} catch (Exception e) {
    System.out.println("There is some error MathUtil.round: " + e.getMessage());
...
intround(double a, int rounding_style)
Returns the mathematically rounded part of a double value.
throw new UnsupportedOperationException("not yet implemented");
doubleround(double amount)
round
double tmp1 = amount;
long factor = (long) Math.pow(10, 2);
tmp1 = tmp1 * factor;
long tmp = Math.round(Math.abs(tmp1));
amount = ((amount < 0 && tmp != 0) ? -1 : 1) * ((double) tmp / factor);
String[] strArr = String.valueOf(amount).split("\\.");
return amount;
doubleround(double amt, int digits)
round
return (Math.round(Math.pow(10, digits) * amt)) / Math.pow(10, digits);