Example usage for java.math BigDecimal ROUND_HALF_UP

List of usage examples for java.math BigDecimal ROUND_HALF_UP

Introduction

In this page you can find the example usage for java.math BigDecimal ROUND_HALF_UP.

Prototype

int ROUND_HALF_UP

To view the source code for java.math BigDecimal ROUND_HALF_UP.

Click Source Link

Document

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

Usage

From source file:Main.java

public static double divide(double v1, double v2, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }//from   ww w. jav a 2 s. c o  m
    BigDecimal b = new BigDecimal(Double.toString(v1));
    BigDecimal one = new BigDecimal(v2);
    return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

/**
 * H = -[0.5 * lg(0.5) + 0.4*lg(0.4) + 0.1*lg(0.1)]
 *///from   w w  w.j  a  v  a2s  . c  o  m
public static BigDecimal calculateEntropy(double... probabilities) {
    BigDecimal res = BigDecimal.ZERO;

    for (double singleProbability : probabilities) {

        BigDecimal singleEntropy = BigDecimal.valueOf(singleProbability)
                .multiply(BigDecimal.valueOf(lg2(singleProbability)));

        res = res.add(singleEntropy);
    }

    return res.negate().setScale(ENTROPY_SCALE, BigDecimal.ROUND_HALF_UP);
}

From source file:Main.java

public static String getNormal(String txamt) {
    String str = txamt;/*from   w  w  w  .  java 2s  . c  om*/
    if (str != null) {
        try {
            String sum = "";
            int index = 0;
            char c = str.charAt(index);
            while (c == '0') {
                index++;
                c = str.charAt(index);
            }
            sum = str.substring(index);
            double i = Double.parseDouble(sum);
            i = i / 100;
            BigDecimal bg = new BigDecimal(i);
            double j = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            sum = "" + j;
            return sum;

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static double round(double d) {
    if (Double.isNaN(d) || Double.isInfinite(d))
        return d;
    int digits = leadingDigits(d);
    int roundToDecimal = Math.min(MAXDIGIT - digits, MAXDECIMAL);
    double rounded = BigDecimal.valueOf(d).setScale(roundToDecimal, BigDecimal.ROUND_HALF_UP).doubleValue();
    return rounded;
}

From source file:Main.java

public static String convertOneDecimal(double price) {
    BigDecimal decimal = new BigDecimal(price);
    return decimal.setScale(1, BigDecimal.ROUND_HALF_UP).toString();
}

From source file:Main.java

public static String convertTwoDecimal(double price) {
    BigDecimal decimal = new BigDecimal(price);
    return decimal.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
}

From source file:Main.java

public static Double div(Double v1, Double v2, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }/*from   w  w w .j  a  v a  2 s.  c  om*/
    BigDecimal b1 = new BigDecimal(v1.toString());
    BigDecimal b2 = new BigDecimal(v2.toString());
    return Double.valueOf(b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue());
}

From source file:Main.java

public static float formatFloat(float floatValue, int newScale) {
    BigDecimal b = new BigDecimal(floatValue);
    return b.setScale(newScale, BigDecimal.ROUND_HALF_UP).floatValue();
}

From source file:Main.java

public static double add(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.add(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

public static double mul(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.multiply(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}