public static Number convertNumberSmartly()

in dekaf-jdbc/src/utils/Numbers.java [64:103]


  public static Number convertNumberSmartly(final BigDecimal decimal) {
    if (decimal == null) return null;
    if (decimal.equals(Numbers.DECIMAL_ZERO)) return BYTE_ZERO;

    final int precision = decimal.precision();
    final int scale = decimal.scale();

    Number num;
    if (scale == 0) {
      try {
        if (precision <= 19 && decimal.compareTo(Numbers.DECIMAL_MIN_LONG) >= 0
                            && decimal.compareTo(Numbers.DECIMAL_MAX_LONG) <= 0) {
          long v = decimal.longValueExact();
          if (-128 <= v && v <= 127) num = (byte) v;
          else if (-32768 <= v && v <= 32767) num = (short) v;
          else if (Integer.MIN_VALUE <= v && v <= Integer.MAX_VALUE) num = (int) v;
          else num = v;
        }
        else {
          num = decimal.toBigIntegerExact();
        }
      }
      catch (ArithmeticException ae) {
        num = decimal;
      }
    }
    else {
      if (precision <= 6) {
        num = decimal.floatValue();
      }
      else if (precision <= 15) {
        num = decimal.doubleValue();
      }
      else {
        num = decimal;
      }
    }

    return num;
  }