returns a new BigDecimal with correct scale.

@param bd
@return new bd or null
509307
public static BigDecimal setScale(final BigDecimal bd, final int scale) {
        return setScale(bd, scale, BigDecimal.ROUND_HALF_UP);
    }
829856
private static BigDecimal convertBinaryToDecimal(Binary value, int precision, int scale)
  {
    // based on parquet-mr pig conversion which is based on spark conversion... yo dawg?
    if (precision <= 18) {
      ByteBuffer buffer = value.toByteBuffer();
      byte[] bytes = buffer.array();
      int start = buffer.arrayOffset() + buffer.position();
      int end = buffer.arrayOffset() + buffer.limit();
      long unscaled = 0L;
      int i = start;
      while (i < end) {
        unscaled = (unscaled << 8 | bytes[i] & 0xff);
        i++;
      }
      int bits = 8 * (end - start);
      long unscaledNew = (unscaled << (64 - bits)) >> (64 - bits);
      if (unscaledNew <= -Math.pow(10, 18) || unscaledNew >= Math.pow(10, 18)) {
        return new BigDecimal(unscaledNew);
      } else {
        return BigDecimal.valueOf(unscaledNew / Math.pow(10, scale));
      }
    } else {
      return new BigDecimal(new BigInteger(value.getBytes()), scale);
    }
  }
664460
public static Decimal fromUnscaledBytes(int precision, int scale, byte[] bytes) {
		if (precision > MAX_COMPACT_PRECISION) {
			BigDecimal bd = new BigDecimal(new BigInteger(bytes), scale);
			return new Decimal(precision, scale, -1, bd);
		}
		assert bytes.length == 8;
		long l = 0;
		for (int i = 0; i < 8; i++) {
			l <<= 8;
			l |= (bytes[i] & (0xff));
		}
		return new Decimal(precision, scale, l, null);
	}
815063
public BigDecimal negate() {
        if (intCompact == INFLATED) {
            return new BigDecimal(intVal.negate(), INFLATED, scale, precision);
        } else {
            return valueOf(-intCompact, scale, precision);
        }
    }
517854
public final BigDecimal getDecimalAsBigDecimal(int columnIndex) {
        validateColumnType(columnIndex, VoltType.DECIMAL);
        final int position = m_buffer.position();
        m_buffer.position(getOffset(columnIndex));
        final BigDecimal bd = VoltDecimalHelper.deserializeBigDecimal(m_buffer);
        m_buffer.position(position);
        m_wasNull = bd == null ? true : false;
        return bd;
    }
508434
public static BigDecimal inverse(final BigDecimal value, final int scale) {
        return inverse(value, scale, BigDecimal.ROUND_HALF_UP);
    }
564966
public PackedDecimal setScale(int n, RoundingMode mode) {
        BigDecimal result = toBigDecimal().setScale(n, mode);
        return PackedDecimal.valueOf(result);
    }
907870
public static BigDecimal toBigDecimal(String number) {
		return (null == number) ? BigDecimal.ZERO : new BigDecimal(number);
	}
361630
public static BigDecimal bd(final String val) {
        return val != null && val.length() > 0 ? new BigDecimal(val) : null;
    }
211855
public static Function<Object,BigDecimal> bigDecimal(final String methodName, final Object... optionalParameters) {
        return methodForBigDecimal(methodName, optionalParameters);
    }
682015
@Override
    public BigDecimal getBigDecimal(int index) {
        indexCheck();
        return (BigDecimal) rows.get(row).getValue(index);
    }
202768
public static Decimal sround(Decimal b0, int r) {
		if (r >= b0.scale) {
			return b0;
		}

		BigDecimal b2 = b0.toBigDecimal().movePointRight(r)
				.setScale(0, RoundingMode.HALF_UP)
				.movePointLeft(r);
		int p = b0.precision;
		int s = b0.scale;
		if (r < 0) {
			return fromBigDecimal(b2, Math.min(38, 1 + p - s), 0);
		} else {  // 0 <= r < s
			return fromBigDecimal(b2, 1 + p - s + r, r);
		}
	}
13040
@Override
    public BigDecimal fromBytes(Class targetClass, byte[] bytes)
    {
        if (bytes == null)
        {
            return null;
        }
        int scale = (((bytes[0]) << 24) | ((bytes[1] & 0xff) << 16) | ((bytes[2] & 0xff) << 8) | ((bytes[3] & 0xff)));

        byte[] bibytes = Arrays.copyOfRange(bytes, 4, bytes.length);

        BigInteger bi = new BigInteger(bibytes);

        return new BigDecimal(bi, scale);
    }
384302
private static BigDecimal getBigDecimalOrNull(Object value) {
            if ( !(value instanceof Number || value instanceof String) ) {
                return null;
            }
            if ( !BigDecimal.class.isAssignableFrom( value.getClass() ) ) {
                if ( value instanceof Long || value instanceof Integer || value instanceof Short || value instanceof Byte ||
                     value instanceof AtomicLong || value instanceof AtomicInteger ) {
                    value = new BigDecimal( ((Number) value).longValue(), MathContext.DECIMAL128 );
                } else if ( value instanceof BigInteger ) {
                    value = new BigDecimal( ((BigInteger) value).toString(), MathContext.DECIMAL128 );
                } else if ( value instanceof String ) {
                    // we need to remove leading zeros to prevent octal conversion
                    value = new BigDecimal( ((String) value).replaceFirst("^0+(?!$)", ""), MathContext.DECIMAL128 );
                } else {
                    value = new BigDecimal( ((Number) value).doubleValue(), MathContext.DECIMAL128 );
                }
            }
            return (BigDecimal) value;
        }
149648
private BigDecimal convertValue(final Object value) {
		if (value == null) {
			return null;
		} else if (value instanceof BigDecimal) {
			return (BigDecimal) value;
		}

		// Try and convert "String" value
		String dataString = value.toString();
		if (Util.empty(dataString)) {
			return null;
		}

		try {
			return new BigDecimal(dataString);
		} catch (NumberFormatException ex) {
			throw new SystemException(
					"Could not convert data of type " + value.getClass() + " with String value "
					+ dataString + " to BigDecimal", ex);
		}
	}
1048596
public BigDecimal toBigDecimal() {

        StringBuilder sb = new StringBuilder();
        this.createNumber(sb);
        return new BigDecimal(sb.toString());

    }
532922
public T divide(T divisor) {
        if (divisor == null) {
            throw new IllegalArgumentException("invalid (null) divisor");
        }
        BigDecimal quotient = this.value.divide(divisor.value, ROUND_BEHAVIOR);

        return newInstance(quotient, this.value.scale());
    }