Example usage for java.math RoundingMode HALF_UP

List of usage examples for java.math RoundingMode HALF_UP

Introduction

In this page you can find the example usage for java.math RoundingMode HALF_UP.

Prototype

RoundingMode HALF_UP

To view the source code for java.math RoundingMode 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

/**
 * @param currentSpeed speed int byte per second
 * @return String with unit such as  3.2Gb/s  200Mb/s  ,...
 *///from   w ww .  ja  v  a 2  s.  co m
public static String getSpeedInAutoUnit(double currentSpeed) {
    String temp = "0.0 B/s";
    df.setRoundingMode(RoundingMode.HALF_UP);

    if (currentSpeed / (1024 * 1024 * 1024) > 1) {//GB
        temp = df.format(currentSpeed / (1024 * 1024 * 1024)) + "GB/s";
    } else if (currentSpeed / (1024 * 1024) > 1) {//MB
        temp = df.format(currentSpeed / (1024 * 1024)) + "MB/s";
    } else if (currentSpeed / (1024) > 1) {//Kb
        temp = df.format(currentSpeed / (1024)) + "KB/s";
    } else if (currentSpeed > 0) {//b
        temp = df.format(currentSpeed / (1024)) + "B/s";
    }

    return temp;
}

From source file:Main.java

/**
 * Round double value with low error/*from   w  w  w .j  a v a2 s.co m*/
 *
 * @param value  Value
 * @param places decimal
 * @return double
 */
static double round(double value, int places) {
    // check if places below 0
    places = Math.max(0, places);
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

From source file:Main.java

/**
 * Rounds a double via passed in amount and places
 * @param value/* w w w  .  j  a  va2s  .com*/
 * @param places
 * @return
 */
public static double round(double value, int places) {
    if (places < 0)
        throw new IllegalArgumentException();

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

From source file:Main.java

public static BigDecimal avg(final List<BigDecimal> list) {
    return list.size() > 0 ? sum(list).setScale(10, RoundingMode.HALF_UP).divide(new BigDecimal(list.size()),
            RoundingMode.HALF_UP) : BigDecimal.ZERO;
}

From source file:Main.java

public static DecimalFormat createDecimalFormat(int minInt, int maxInt, int minFract, int maxFract,
        char separator) {
    return createDecimalFormat(minInt, maxInt, minFract, maxFract, separator, RoundingMode.HALF_UP);
}

From source file:Main.java

@SuppressWarnings("SameParameterValue")
private static double round(double value, int places) {
    if (places < 0) {
        throw new IllegalArgumentException();
    }/*from  ww  w .ja  v  a 2 s.  c o m*/

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

From source file:org.apache.fineract.portfolio.servicecharge.util.ServiceChargeOperationUtils.java

public static BigDecimal divideAndMultiplyNonZeroValues(BigDecimal operand, BigDecimal divisor,
        BigDecimal multiplicand) {
    if (operand == null) {
        return BigDecimal.ONE;
    }/*  w w w.j ava2 s.  c o  m*/
    if (divisor != null && !divisor.equals(BigDecimal.ZERO)) {
        operand = operand.divide(divisor, RoundingMode.HALF_UP);
    }
    if (multiplicand != null) {
        operand = operand.multiply(multiplicand);
    }
    return operand;
}

From source file:com.floreantpos.util.NumberUtil.java

public static double roundToTwoDigit(double value) {
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(2, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

From source file:org.apache.fineract.portfolio.servicecharge.util.ServiceChargeOperationUtils.java

public static BigDecimal divideNonZeroValues(BigDecimal operand, BigDecimal divisor) {
    if (operand == null) {
        return BigDecimal.ONE;
    }//from  ww  w. j av  a 2  s  . c om
    if (divisor != null && !divisor.equals(BigDecimal.ZERO)) {
        operand = operand.divide(divisor, RoundingMode.HALF_UP);
    }
    return operand;
}

From source file:ymanv.forex.provider.AProvider.java

protected static BigDecimal divide(BigDecimal dividend, BigDecimal divisor) {
    return dividend.multiply(PRECISION_QUANTITY).divide(divisor, RoundingMode.HALF_UP)
            .divide(PRECISION_QUANTITY);
}