Java Utililty Methods BigDecimal

List of utility methods to do BigDecimal

Description

The list of methods to do BigDecimal are organized into topic(s).

Method

BigDecimalstdBigDecimal(List vector, BigDecimal avg)
std Big Decimal
BigDecimal accum = BigDecimal.ZERO;
BigDecimal accum2 = BigDecimal.ZERO;
BigDecimal len = BigDecimal.valueOf(vector.size());
for (Double float1 : vector) {
    BigDecimal val = BigDecimal.valueOf(float1);
    BigDecimal dev = val.subtract(avg);
    accum = accum.add(dev.pow(2));
    accum2 = accum2.add(dev);
...
BigDecimalstddev(List numbers, boolean biasCorrected, MathContext context)
Returns the standard deviation of the numbers.
BigDecimal stddev;
int n = numbers.size();
if (n > 0) {
    if (n > 1) {
        stddev = sqrt(var(numbers, biasCorrected, context));
    } else {
        stddev = BigDecimal.ZERO;
} else {
    stddev = BigDecimal.valueOf(Double.NaN);
return stddev;
MapstdDevMaxFromMapField(List> dataList, String fieldName, BigDecimal stdDevMultiplier)
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
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)
...
BigDecimalStr2BigDecimal(String a)
Str Big Decimal
try {
    return (new BigDecimal(a));
} catch (Exception e) {
    return BigDecimal.valueOf(0);
BigDecimalstr2BigDicimal(String strbigdecimal)
str Big Dicimal
if (strbigdecimal == null || strbigdecimal.trim().length() == 0)
    return null;
BigDecimal bd = null;
try {
    bd = new BigDecimal(strbigdecimal);
} catch (Exception e) {
    return null;
return bd;
BigDecimalstripAndOrRescale(BigDecimal value)
strip And Or Rescale
value = value.stripTrailingZeros();
try {
    value.toBigIntegerExact();
    value = value.setScale(1, RoundingMode.UNNECESSARY);
} catch (ArithmeticException ignored) {
return value;
BigDecimalstripTrailingZeros(BigDecimal bigDecimal)
strip Trailing Zeros
if (bigDecimal.compareTo(BigDecimal.ZERO) == 0) {
    return BigDecimal.ZERO;
} else {
    return bigDecimal.stripTrailingZeros();
BigDecimalstripTrailingZeros(BigDecimal value)
Workaround for bug #6480539: BigDecimal.stripTrailingZeros() has no effect on zero itself ("0.0").
return (value.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : value.stripTrailingZeros());
BigDecimalstripTrailingZeros(final BigDecimal decimal)
Strips trailing zeros from a BigDecimal.
if (decimal.compareTo(ZERO) == 0) {
    return ZERO;
} else {
    return decimal.stripTrailingZeros();
BigDecimalstrToBigDecimal(final String arg)
Converte para java.math.BigDecimal um String de inteiros sem ponto ou virgula.
String value = null;
if (arg != null) {
    char chars[] = arg.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        if ('0' != chars[i]) {
            value = arg.substring(i);
            break;
    if (value != null) {
        value = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2);
return new BigDecimal(value);