code: public BigDecimal toBigDecimal(Object val) {
      if (val instanceof BigDecimal) {
        return roundBigDecimal((BigDecimal) val);
      }
      else if (val == null) {
        return BigDecimal.ZERO;
      }
      else if (val instanceof String) {
        String string = ((String) val).trim();
        if ("".equals(string)) {
          return BigDecimal.ZERO;
        }
        return roundBigDecimal(new BigDecimal(string, getMathContext()));
      }
      else if (val instanceof Double) {
        if (!Double.isNaN(((Double) val).doubleValue())) {
          return roundBigDecimal(new BigDecimal(val.toString(), getMathContext()));
        }
        else {
          return BigDecimal.ZERO;
        }
      }
      else if (val instanceof Number) {
        return roundBigDecimal(new BigDecimal(val.toString(), getMathContext()));
      }
      else if (val instanceof Character) {
        int i = ((Character) val).charValue();
        return new BigDecimal(i);
      }
  
      throw new ArithmeticException("BigDecimal coercion: " + val.getClass().getName() + ":(" + val + ")");
    }
docs: Get a BigDecimal from the object passed. Null and empty string maps to
  zero.
  
  @param val
  the object to be coerced.
  @return a BigDecimal.
  @throws NullPointerException
  if val is null and mode is strict.