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

/** Returns Map with total, squaredTotal, count, average, stdDev, maximum; fieldName field in Maps must have type BigDecimal;
 * if count of non-null fields is less than 2 returns null as cannot calculate a standard deviation */
public static Map<String, BigDecimal> stdDevMaxFromMapField(List<Map<String, Object>> dataList,
        String fieldName, BigDecimal stdDevMultiplier) {
    BigDecimal total = BigDecimal.ZERO;
    BigDecimal squaredTotal = BigDecimal.ZERO;
    int count = 0;
    for (Map<String, Object> dataMap : dataList) {
        if (dataMap == null)
            continue;
        BigDecimal value = (BigDecimal) dataMap.get(fieldName);
        if (value == null)
            continue;
        total = total.add(value);//from   w ww  .  j a  v a2s .  c om
        squaredTotal = squaredTotal.add(value.multiply(value));
        count++;
    }
    if (count < 2)
        return null;

    BigDecimal countBd = new BigDecimal(count);
    BigDecimal average = total.divide(countBd, BigDecimal.ROUND_HALF_UP);
    double totalDouble = total.doubleValue();
    BigDecimal stdDev = new BigDecimal(Math
            .sqrt(Math.abs(squaredTotal.doubleValue() - ((totalDouble * totalDouble) / count)) / (count - 1)));

    Map<String, BigDecimal> retMap = new HashMap<>(6);
    retMap.put("total", total);
    retMap.put("squaredTotal", squaredTotal);
    retMap.put("count", countBd);
    retMap.put("average", average);
    retMap.put("stdDev", stdDev);

    if (stdDevMultiplier != null)
        retMap.put("maximum", average.add(stdDev.multiply(stdDevMultiplier)));

    return retMap;
}

From source file:ext.paycenter.PayService.java

/**
 * ?token/* ww w .  ja va 2  s . c  o  m*/
 * 
 * @param token token
 * @param email ?
 * @param payment ????
 * @param serveduration ?
 * @param isIgnoreLocked ??  true
 * @return
 */
public static TransferResult transferByToken(String token, String email, BigDecimal payment, Long serveduration,
        boolean isIgnoreLocked) {
    if (StringUtils.isBlank(token) || StringUtils.isBlank(email) || null == payment || null == serveduration) {
        throw new IllegalArgumentException("??token = " + token + ", email = " + email
                + ", payment = " + payment + ", serveduration = " + serveduration);
    }

    String paymentString = payment.setScale(2, BigDecimal.ROUND_HALF_UP).toString();

    PCResult<Void> result = PCClient.transferax(token, email, paymentString, String.valueOf(serveduration),
            isIgnoreLocked);
    if (result.balanceNotEnough()) {
        return TransferResult.BALANCE_NOT_ENOUGH;
    } else if (result.isSuccess()) {
        return TransferResult.SUCCESS;
    } else if (result.noMatchData()) {
        return TransferResult.INVALID_TOKEN;
    } else if (result.accountLocked()) {
        return TransferResult.ACCOUNT_LOCKED;
    } else {
        LOGGER.error("call transferax error result = " + result);
    }

    return TransferResult.UNKNOWN_ERROR;
}

From source file:de.tsystems.mms.apm.performancesignature.util.PerfSigUIUtils.java

public static BigDecimal round(final double d, final int scale) {
    try {/* w w  w  .j a  v a2  s  . co m*/
        return BigDecimal.valueOf(d).setScale(d % 1 == 0 ? 0 : scale, BigDecimal.ROUND_HALF_UP);
    } catch (NumberFormatException ex) {
        if (Double.isInfinite(d)) {
            return BigDecimal.valueOf(d);
        } else {
            return BigDecimal.ZERO;
        }
    }
}

From source file:Main.java

/**
 * Write a {@link BigDecimal} value into XML output.
 *
 * @param value//from www  . ja va  2 s.c  om
 * value to write
 *
 * @param min
 * minimal value
 *
 * @param max
 * maximal value
 *
 * @param precision
 * number of decimal places
 *
 * @return
 * XML string
 *
 * @throws IllegalArgumentException
 * if a validation error occured
 */
private static String printDecimal(BigDecimal value, BigDecimal min, BigDecimal max, int precision,
        boolean zeroIncluded) {
    if (value == null) {
        throw new IllegalArgumentException("The provided double value NULL is invalid!");
    }
    value = value.setScale(precision, BigDecimal.ROUND_HALF_UP);

    if (min != null && value.compareTo(min) <= 0) {
        if (!zeroIncluded || !BigDecimal.ZERO.equals(value)) {
            throw new IllegalArgumentException(
                    "The provided double value " + value + " is too small (minimum is " + min + ")!");
        }
    }
    if (max != null && value.compareTo(max) >= 0) {
        throw new IllegalArgumentException(
                "The provided double value " + value + " is too high (maximum is " + max + ")!");
    }

    return DatatypeConverter.printDecimal(value);
}

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

public static BigDecimal getBigDecimal(double value, int scale) {

    if (!Double.isNaN(value)) {
        BigDecimal decimal = new BigDecimal(value);
        return decimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
    } else {/*w  w w.jav a  2s  .c  om*/
        return null;
    }
}

From source file:org.eyeseetea.malariacare.utils.Utils.java

public static String round(float base, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(base));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    if (decimalPlace == 0)
        return Integer.toString((int) bd.floatValue());
    return Float.toString(bd.floatValue());
}

From source file:kenh.expl.functions.Round.java

public double process(double number, int scale) {
    BigDecimal b = new BigDecimal(number);
    number = b.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    return number;
}

From source file:com.create.databind.MoneySerializer.java

@Override
public void serialize(BigDecimal value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
        throws IOException {
    final String moneyValue = value.setScale(CURRENCY_SCALE, BigDecimal.ROUND_HALF_UP).toString();
    jsonGenerator.writeString(moneyValue);
}

From source file:com.create.databind.MoneyDeserializer.java

@Override
public BigDecimal deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException {
    final String value = jsonParser.getValueAsString();
    return new BigDecimal(value).setScale(CURRENCY_SCALE, BigDecimal.ROUND_HALF_UP);
}

From source file:ch.algotrader.util.RoundUtil.java

public static BigDecimal getBigDecimal(double value) {

    if (Double.isNaN(value) || Double.isInfinite(value)) {
        return null;
    } else {//from ww w. ja va2s  .  com
        BigDecimal decimal = new BigDecimal(value);
        CommonConfig commonConfig = ConfigLocator.instance().getCommonConfig();
        return decimal.setScale(commonConfig.getPortfolioDigits(), BigDecimal.ROUND_HALF_UP);
    }
}