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 void divideBigDecimalsInMap(Map<String, Object> baseMap, BigDecimal divisor) {
    if (baseMap == null || divisor == null || divisor.doubleValue() == 0.0)
        return;/*from   w  w w  . j a  va2  s  .  co  m*/
    for (Map.Entry<String, Object> entry : baseMap.entrySet()) {
        if (!(entry.getValue() instanceof BigDecimal))
            continue;
        BigDecimal baseVal = (BigDecimal) entry.getValue();
        entry.setValue(baseVal.divide(divisor, BigDecimal.ROUND_HALF_UP));
    }
}

From source file:Main.java

public static double round(double value, int nDecimals) {
    BigDecimal r = new BigDecimal(value);
    return r.setScale(nDecimals, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

public static double transformTwoDecimalDoubleNumber(double number) {
    BigDecimal bigD = new BigDecimal(number);
    bigD = bigD.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
    return bigD.doubleValue();
}

From source file:NumberUtil.java

/**
 * Returns the BigDecimal value n with exactly
 * 'prec' decimal places./*  ww w  . ja v a 2s.  c o m*/
 * Zeroes are padded to the right of the decimal
 * point if necessary.
 */
public static BigDecimal format(BigDecimal n, int prec) {
    return n.setScale(prec, BigDecimal.ROUND_HALF_UP);
}

From source file:Main.java

/**
 * Round the given value to the specified number of decimal places. The
 * value is rounded using the {@link BigDecimal#ROUND_HALF_UP} method.
 * //from ww w  .j a v a 2s. c o m
 * @param x the value to round.
 * @param scale the number of digits to the right of the decimal point.
 * @return the rounded value.
 * @since 1.1
 */
public static double round(double x, int scale) {
    return round(x, scale, BigDecimal.ROUND_HALF_UP);
}

From source file:com.fengduo.bee.commons.util.NumberParser.java

/**
 * /*from www  . j  a v a2 s.co m*/
 * 
 * @return
 */
public static String fromUsage(long free, long total) {
    Double d = new BigDecimal(free * 100 / total).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
    return String.valueOf(d);
}

From source file:Main.java

public static double round(double v, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }//w ww  .jav a  2 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

/**
 * Divides two decimals and applies the given scale and a ROUND_HALF_UP. This method should be used only for the final result
 * calculation. For example if we have something like this: (axb)/c the rules should be applied to the result of the division
 * only and not all the computations that give us the final result.
 * /*from  www .  j  av  a 2s.  co m*/
 * @param dividend the dividend
 * @param divisor the divisor
 * @param scale the scale for the result
 * @return the result of the division after the scale and rounding are applied
 */
public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, int scale) {

    BigDecimal result = BigDecimal.ZERO;

    if (dividend != null && divisor != null && !divisor.equals(BigDecimal.ZERO)) {
        result = dividend.divide(divisor, scale, BigDecimal.ROUND_HALF_UP);
    }

    return result;
}

From source file:Main.java

/**
 * Multiplies two decimals and applies the given scale and a ROUND_HALF_UP. This method should be used only for the final result
 * calculation.For example if we have something like this: (axb)/c the rules should be applied to the result of the division
 * only and not all the computations that give us the final result.
 * //  w  ww.j a  v a  2 s  . c o m
 * @param multiplier1 first multiplier
 * @param multiplier2 second multiplier
 * @param scale the scale fo the result
 * @return the result of the multiplication after scale and rounding are applied
 */
public static BigDecimal multiply(BigDecimal multiplier1, BigDecimal multiplier2, int scale) {

    BigDecimal result = BigDecimal.ZERO;

    if (multiplier1 != null && multiplier2 != null) {
        result = multiplier1.multiply(multiplier2);
    }

    result = result.setScale(scale, BigDecimal.ROUND_HALF_UP);
    return result;
}

From source file:com.algoTrader.util.RoundUtil.java

public static BigDecimal getBigDecimal(double value) {

    if (!Double.isNaN(value)) {
        BigDecimal decimal = new BigDecimal(value);
        return decimal.setScale(PORTFOLIO_DIGITS, BigDecimal.ROUND_HALF_UP);
    } else {//from  w  ww  . j a  va2 s . c o m
        return null;
    }
}