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 add(double v1, double v2) {
    BigDecimal a = new BigDecimal(v1);
    BigDecimal b = new BigDecimal(v2);
    BigDecimal c = a.add(b);/*from   www.  ja va2  s .c o  m*/
    return c.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

public static double sub(double v1, double v2) {
    BigDecimal a = new BigDecimal(v1);
    BigDecimal b = new BigDecimal(v2);
    BigDecimal c = a.subtract(b);
    c.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    return c.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

public static double changep2(double i) {
    // TODO Auto-generated method stub
    BigDecimal b = new BigDecimal(i);
    int saveBitNum = 2;
    double c = b.setScale(saveBitNum, BigDecimal.ROUND_HALF_UP).doubleValue();
    return c;//from w  w w .  j a v  a2s.c  om
}

From source file:Main.java

public static String keepZeroSecimal(String numberStr) {
    double number = Double.parseDouble(numberStr);
    BigDecimal b = new BigDecimal(number);

    String ss = b.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue() + "";

    return ss;//w w  w . j ava  2s.  co m
}

From source file:Main.java

public static double mul(double v1, double v2) {
    BigDecimal a = new BigDecimal(v1);
    BigDecimal b = new BigDecimal(v2);
    BigDecimal c = a.multiply(b);
    return c.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

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

From source file:Main.java

public static String keepNSecimal(String numberStr, int n) {
    double number = Double.parseDouble(numberStr);
    BigDecimal b = new BigDecimal(number);
    String ss = b.setScale(n, BigDecimal.ROUND_HALF_UP).doubleValue() + "";
    int pointIndex = ss.lastIndexOf(".");
    int fractionCount = ss.length() - pointIndex - 1;
    for (int i = 0; i < n - fractionCount; i++) {
        ss += 0;/* w ww . j av  a 2  s  .  c om*/
    }
    return ss;
}

From source file:Main.java

private static BigDecimal findRatio(int width, int height) {
    BigDecimal bdWidth = BigDecimal.valueOf(width);
    BigDecimal bdHeight = BigDecimal.valueOf(height);
    return bdWidth.divide(bdHeight, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(1000L));
}

From source file:Main.java

public static BigDecimal round(double value, int decimals) {
    BigDecimal valueBigDecimal = new BigDecimal(value);
    return valueBigDecimal.setScale(decimals, BigDecimal.ROUND_HALF_UP);
}

From source file:Main.java

private static BigDecimal round(double value, int decimals) {
    BigDecimal valueBigDecimal = new BigDecimal(value);
    return valueBigDecimal.setScale(decimals, BigDecimal.ROUND_HALF_UP);
}