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 sub(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.subtract(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

public static BigDecimal calculateDivide(BigDecimal numerator, BigDecimal denominator) {
    BigDecimal result = BigDecimal.ZERO;
    if (denominator.compareTo(BigDecimal.ZERO) != 0) {
        result = numerator.divide(denominator, 3, BigDecimal.ROUND_HALF_UP);
    }/* w ww .  jav a2  s. c  o m*/
    return result;
}

From source file:Main.java

public static String fileSize2String(long size, int scale) {
    float result;
    String unit;/*from   w ww .j av a2 s  . co  m*/
    if (size <= 0) {
        result = 0;
        unit = "B";
    } else if (size < KB) {
        result = size;
        unit = "B";
    } else if (size < MB) {
        result = size / KB;
        unit = "KB";
    } else if (size < GB) {
        result = size / MB;
        unit = "MB";
    } else {
        result = size / GB;
        unit = "GB";
    }
    BigDecimal bg = new BigDecimal(result);
    float f1 = bg.setScale(scale, BigDecimal.ROUND_HALF_UP).floatValue();
    return f1 + unit;
}

From source file:Main.java

public static double round(double d, int decimalPlace) {
    try {//from  w w  w . jav  a2  s .c o  m
        BigDecimal bd = new BigDecimal(Double.toString(d));
        bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    } catch (NumberFormatException e) {
        //on NaN or +/- infinity return the value
        return d;
    }
}

From source file:Main.java

public static BigDecimal calculatePercent(BigDecimal numerator, BigDecimal denominator) {
    BigDecimal result = BigDecimal.ZERO;
    if (numerator != null && denominator != null && (denominator.compareTo(BigDecimal.ZERO) != 0)) {
        result = numerator.divide(denominator, 3, BigDecimal.ROUND_HALF_UP).movePointRight(2);
    }/*from   ww w .ja  v a2s  .  co m*/
    return result;
}

From source file:Main.java

public static BigDecimal tangent(BigDecimal x) {

    BigDecimal sin = sine(x);/* w  w  w. j a va 2 s  .c o  m*/
    BigDecimal cos = cosine(x);

    return sin.divide(cos, SCALE, BigDecimal.ROUND_HALF_UP);
}

From source file:json_cmp.Comparer.java

public static void FloatingtoInt(ObjectNode obj, String str) {
    //System.out.println("   " + str + " " + obj.path(str).asDouble());
    BigDecimal temp = new BigDecimal(obj.path(str).asDouble()).setScale(0, BigDecimal.ROUND_HALF_UP);
    //int temp = (int) Math.ceil(obj.path(str).asDouble());
    ((ObjectNode) obj).put(str, temp.toString());
    int output = (int) obj.path(str).asDouble();
    ((ObjectNode) obj).put(str, output);
    //        System.out.println(mediaObj.path(str1).path(str2));
}

From source file:Main.java

public static BigDecimal round(double number, int decimal) {
    return new BigDecimal(number).setScale(decimal, BigDecimal.ROUND_HALF_UP);
}

From source file:Main.java

public static BigDecimal setDecimalDigit(BigDecimal number, int digit, boolean setNullIndicator) {
    BigDecimal returnNum = BigDecimal.ZERO;
    if (number != null) {
        if ((number.compareTo(BigDecimal.ZERO) == 0) && setNullIndicator) {
            return null;
        }//w  ww  .ja v  a  2 s.c om
        returnNum = number.setScale(digit, BigDecimal.ROUND_HALF_UP);
    }
    return returnNum;
}

From source file:Main.java

public static String getFormatSize(double size) {
    double kiloByte = size / 1024;
    if (kiloByte < 1) {
        return size + "Byte";
    }/*w w w  . j  a v  a  2s.com*/

    double megaByte = kiloByte / 1024;
    if (megaByte < 1) {
        BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
        return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
    }

    double gigaByte = megaByte / 1024;
    if (gigaByte < 1) {
        BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
        return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
    }

    double teraBytes = gigaByte / 1024;
    if (teraBytes < 1) {
        BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
        return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
    }
    BigDecimal result4 = new BigDecimal(teraBytes);
    return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
}