Example usage for java.math BigDecimal BigDecimal

List of usage examples for java.math BigDecimal BigDecimal

Introduction

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

Prototype

public BigDecimal(long val) 

Source Link

Document

Translates a long into a BigDecimal .

Usage

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 {/*  www. j av  a  2  s  .c om*/
        return null;
    }
}

From source file:Main.java

public static String formatBalance(Double balance, String curr) {
    return formatBalance(new BigDecimal(balance), curr);
}

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 ww  w.  j  a  va2  s  .  c  o  m*/
        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:net.hamnaberg.json.ValueFactory.java

@SuppressWarnings("unchecked")
public static Value createValue(Object value) {
    if (value == null) {
        return ValueImpl.NULL;
    }//from  ww w .  j  a v a  2 s .  c  om
    if (value instanceof Number && !(value instanceof BigDecimal)) {
        value = new BigDecimal(value.toString());
    }
    if (value instanceof Optional) {
        return createValue(((Optional) value).orElse(null));
    }
    if (!checkValue(value)) {
        return new ValueImpl(value.toString());
    }
    return new ValueImpl(value);
}

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:be.error.rpi.ebus.Support.java

public static String encodeDATA2c(BigDecimal data) {
    return new String(encodeHex(
            encodeInt((short) (data.multiply(new BigDecimal(16)).setScale(0, ROUND_FLOOR).floatValue()))));
}

From source file:ec.edu.distri.clientejava.protocolo.model.Canal.java

public Canal() {
    costo = new BigDecimal(BigInteger.ZERO);
}

From source file:com.yoncabt.ebr.executor.jasper.Convert.java

private static <T> T str2Number(String val, Class<T> valueClass) {
    if (StringUtils.isBlank(val)) {
        return null;
    }/*  w  w  w  .j  av  a2s  . c  o m*/
    if (valueClass == Integer.class) {
        return (T) Integer.valueOf(val);
    }
    if (valueClass == Long.class) {
        return (T) Long.valueOf(val);
    }
    if (valueClass == Short.class) {
        return (T) Short.valueOf(val);
    }
    if (valueClass == Double.class) {
        return (T) Double.valueOf(val);
    }
    if (valueClass == BigDecimal.class) {
        return (T) new BigDecimal(val);
    }
    throw new IllegalArgumentException(val.getClass() + "->" + valueClass.getName());
}

From source file:com.ebay.cloud.cms.service.resources.impl.MetadataValidator.java

public static void validateNumber(Number value, String key) {
    if (value != null) {
        CheckConditions.checkArgument(new BigDecimal(value.toString()).compareTo(BigDecimal.ZERO) > 0,
                "Configuration value must be greater than 0 for configuration item %s!", key);
    }/*from   w  ww  . ja  va  2s .c  o m*/
}

From source file:Main.java

private static BigDecimal getBigDecimal(Object number) {
    BigDecimal num = null;//from  w  w  w  .  j  a  v  a2  s.  c  o m
    if (number instanceof BigDecimal) {
        num = (BigDecimal) number;
    } else {
        num = new BigDecimal(number.toString());
    }
    return num;
}