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 String formatNumber(int number) {
    double kilo = ((double) number) / 1000;
    if (kilo > 1) {
        BigDecimal result = new BigDecimal(Double.toString(kilo));
        return result.setScale(1, BigDecimal.ROUND_HALF_UP).toPlainString() + "k";
    } else {/*from   w  w  w  . jav  a  2  s .c  om*/
        return number + "";
    }
}

From source file:Main.java

public static float getDurationToFloat(long duration) {
    float f = 0;//w  w w  . ja v  a  2s.  co  m
    try {
        BigDecimal b = new BigDecimal(Long.toString(duration));
        BigDecimal one = new BigDecimal("1000");
        f = b.divide(one, 1, BigDecimal.ROUND_HALF_UP).floatValue();

    } catch (Exception e) {
        e.printStackTrace();
        f = 0;
    }
    return f;
}

From source file:Main.java

private static Float precision(int decimalPlace, Float d) {

    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}

From source file:Main.java

protected static final String encodingUTF8andChange(byte[] src, int srart, int length) throws Exception {
    byte[] b = new String(src, srart, length, "euc-kr").getBytes("utf-8");
    String str = new String(b).trim();
    return new BigDecimal(str).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
}

From source file:Main.java

public static double[] round2(double[] doubleRes) {
    double[] result = new double[doubleRes.length];
    for (int i = 0; i < doubleRes.length; i++) {
        BigDecimal bg = new BigDecimal(doubleRes[i]);
        result[i] = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }/*from  ww w .j a v a 2  s. co  m*/
    return result;
}

From source file:Main.java

public static Object round(Object value, int places) {
    // if (places < 0) throw new IllegalArgumentException();
    if (places < 0 || !(value instanceof Double)) {
        return value;
    }/*from   ww  w .j a  v a  2s.com*/
    BigDecimal bd = new BigDecimal((Double) value);
    bd = bd.setScale(places, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
}

From source file:Main.java

public static Double div(Double v1, Double v2) {
    BigDecimal b1 = new BigDecimal(v1.toString());
    BigDecimal b2 = new BigDecimal(v2.toString());
    return Double.valueOf(b1.divide(b2, DEF_DIV_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue());
}

From source file:Main.java

public static double round(double d, int decimalPlace) {
    // see the Javadoc about why we use a String in the constructor
    // http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double)
    BigDecimal bd = new BigDecimal(Double.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
}

From source file:Main.java

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

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");
    }//  w  w w  .  j  av a 2 s  .c  o m
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}