Example usage for java.math BigDecimal ZERO

List of usage examples for java.math BigDecimal ZERO

Introduction

In this page you can find the example usage for java.math BigDecimal ZERO.

Prototype

BigDecimal ZERO

To view the source code for java.math BigDecimal ZERO.

Click Source Link

Document

The value 0, with a scale of 0.

Usage

From source file:Main.java

public static BigDecimal sum(final List<BigDecimal> list) {
    BigDecimal total = BigDecimal.ZERO;
    for (final BigDecimal item : list) {
        total = total.add(item);/*w w w  .  j  a  v  a  2 s . co m*/
    }
    return total;
}

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 w w.j  av a 2  s  .  com
        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:Main.java

/**
 * Write a {@link BigDecimal} value into XML output
 * with maximal 8 digits and 2 decimal places.
 *
 * @param value//from   w  ww. ja va  2  s  .  c o m
 * value to write
 *
 * @return
 * XML string
 *
 * @throws IllegalArgumentException
 * if a validation error occured
 */
public static String printDecimal8_2(BigDecimal value) {
    return printDecimal(value, BigDecimal.ZERO, BigDecimal.TEN.pow(8), 2, false);
}

From source file:Main.java

/**
 * Write a positive {@link BigDecimal} value into XML output
 * with maximal 3 decimal places./*from  ww  w  . j  a va  2 s .  c  om*/
 *
 * @param value
 * value to write
 *
 * @return
 * XML string
 *
 * @throws IllegalArgumentException
 * if a validation error occured
 */
public static String printDecimalPositive(BigDecimal value) {
    return printDecimal(value, BigDecimal.ZERO, null, 3, false);
}

From source file:Main.java

/**
 * Write a {@link BigDecimal} value into XML output
 * with maximal 13 digits and 2 decimal places.
 *
 * @param value// w ww .  j a va 2 s  .co m
 * value to write
 *
 * @return
 * XML string
 *
 * @throws IllegalArgumentException
 * if a validation error occured
 */
public static String printDecimal13_2(BigDecimal value) {
    return printDecimal(value, BigDecimal.ZERO, BigDecimal.TEN.pow(13), 2, false);
}

From source file:Main.java

/**
 * Write a {@link BigDecimal} value into XML output,
 * that matches the 'PriceMultiplierType' simple type.
 *
 * @param value//from   w ww .  j  a  va 2s .com
 * value to write
 *
 * @return
 * XML string
 *
 * @throws IllegalArgumentException
 * if a validation error occured
 */
public static String printPriceMultiplier(BigDecimal value) {
    return printDecimal(value, BigDecimal.ZERO, BigDecimal.TEN.pow(2), 1, true);
}

From source file:Main.java

public static void addBigDecimalsInMap(Map<String, Object> baseMap, Map<String, Object> addMap) {
    if (baseMap == null || addMap == null)
        return;//from w  ww  . ja v  a2 s.  c om
    for (Map.Entry<String, Object> entry : addMap.entrySet()) {
        if (!(entry.getValue() instanceof BigDecimal))
            continue;
        BigDecimal addVal = (BigDecimal) entry.getValue();
        Object baseObj = baseMap.get(entry.getKey());
        if (baseObj == null || !(baseObj instanceof BigDecimal))
            baseObj = BigDecimal.ZERO;
        BigDecimal baseVal = (BigDecimal) baseObj;
        baseMap.put(entry.getKey(), baseVal.add(addVal));
    }
}

From source file:Main.java

/**
 * Write a {@link BigDecimal} value into XML output,
 * that matches the type of the "thermalCharacteristic" element.
 *
 * @param value// w ww .j  a v a2 s. c  o m
 * value to write
 *
 * @return
 * XML string
 *
 * @throws IllegalArgumentException
 * if a validation error occured
 */
public static String printThermalCharacteristic(BigDecimal value) {
    return printDecimal(value, BigDecimal.ZERO, new BigDecimal("2000"), 3, false);
}

From source file:Main.java

/**
 * Write a {@link BigDecimal} value into XML output,
 * that matches the type of the 'numberOfRooms' element in "BaseHouse".
 *
 * @param value/*from  ww w.j a v  a 2 s  .  co m*/
 * value to write
 *
 * @return
 * XML string
 *
 * @throws IllegalArgumentException
 * if a validation error occured
 */
public static String printRoomNrForBaseHouse(BigDecimal value) {
    return printDecimal(value, BigDecimal.ZERO, BigDecimal.TEN.pow(3), 1, true);
}

From source file:Main.java

public static BigDecimal calculatePercent(Integer numerator, Integer denominator) {
    BigDecimal result = BigDecimal.ZERO;
    if (numerator != null && denominator != null) {
        return calculatePercent(new BigDecimal(numerator.intValue()), new BigDecimal(denominator.intValue()));
    }// www.  j a v a2s .c  o m
    return result;
}