Java Code Examples for java.math.BigDecimal#shortValue()

The following examples show how to use java.math.BigDecimal#shortValue() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: doma   File: ExpressionEvaluator.java    License: Apache License 2.0 6 votes vote down vote up
protected Object narrowValue(BigDecimal value, Class<?> clazz) {
  if (clazz == BigDecimal.class) {
    return value;
  } else if (clazz == BigInteger.class) {
    return value.toBigInteger();
  } else if (clazz == Double.class || clazz == double.class) {
    return value.doubleValue();
  } else if (clazz == Float.class || clazz == float.class) {
    return value.floatValue();
  } else if (clazz == Long.class || clazz == long.class) {
    return value.longValue();
  } else if (clazz == Integer.class || clazz == int.class) {
    return value.intValue();
  } else if (clazz == Short.class || clazz == short.class) {
    return value.shortValue();
  } else if (clazz == Byte.class || clazz == byte.class) {
    return value.byteValue();
  }
  return assertUnreachable();
}
 
Example 2
Source Project: grpc-nebula-java   File: TypeUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static short shortValue(BigDecimal decimal) {
    if (decimal == null) {
        return 0;
    }

    int scale = decimal.scale();
    if (scale >= -100 && scale <= 100) {
        return decimal.shortValue();
    }

    return decimal.shortValueExact();
}
 
Example 3
Source Project: uavstack   File: TypeUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static short shortValue(BigDecimal decimal) {
    if (decimal == null) {
        return 0;
    }

    int scale = decimal.scale();
    if (scale >= -100 && scale <= 100) {
        return decimal.shortValue();
    }

    return decimal.shortValueExact();
}
 
Example 4
@Override
public short toShort(int index) throws SFException
{
  if (isNull(index))
  {
    return 0;
  }
  BigDecimal bigDecimal = toBigDecimal(index);
  if (bigDecimal.scale() == 0)
  {
    short shortValue = bigDecimal.shortValue();

    if (bigDecimal.compareTo(BigDecimal.valueOf((long) shortValue)) == 0)
    {
      return shortValue;
    }
    else
    {
      throw new SFException(ErrorCode.INVALID_VALUE_CONVERT, logicalTypeStr,
                            "Short", bigDecimal.toPlainString());
    }
  }
  else
  {
    throw new SFException(ErrorCode.INVALID_VALUE_CONVERT, logicalTypeStr,
                          "Short", bigDecimal.toPlainString());
  }
}
 
Example 5
/**
 * Get short from raw binary format.
 *
 * @param columnInfo column information
 * @return short value
 * @throws SQLException if column type doesn't permit conversion
 */
public short getInternalShort(ColumnDefinition columnInfo) throws SQLException {
  if (lastValueWasNull()) {
    return 0;
  }

  long value;
  switch (columnInfo.getColumnType()) {
    case BIT:
      value = parseBit();
      break;
    case TINYINT:
      value = getInternalTinyInt(columnInfo);
      break;
    case SMALLINT:
    case YEAR:
      value = ((buf[pos] & 0xff) + ((buf[pos + 1] & 0xff) << 8));
      if (columnInfo.isSigned()) {
        return (short) value;
      }
      value = value & 0xffff;
      break;
    case INTEGER:
    case MEDIUMINT:
      value = getInternalMediumInt(columnInfo);
      break;
    case BIGINT:
      value = getInternalLong(columnInfo);
      break;
    case FLOAT:
      value = (long) getInternalFloat(columnInfo);
      break;
    case DOUBLE:
      value = (long) getInternalDouble(columnInfo);
      break;
    case DECIMAL:
    case OLDDECIMAL:
      BigDecimal bigDecimal = getInternalBigDecimal(columnInfo);
      rangeCheck(Short.class, Short.MIN_VALUE, Short.MAX_VALUE, bigDecimal, columnInfo);
      return bigDecimal.shortValue();
    case VARSTRING:
    case VARCHAR:
    case STRING:
      value = Long.parseLong(new String(buf, pos, length, StandardCharsets.UTF_8));
      break;
    default:
      throw new SQLException(
          "getShort not available for data field type "
              + columnInfo.getColumnType().getJavaTypeName());
  }
  rangeCheck(Short.class, Short.MIN_VALUE, Short.MAX_VALUE, value, columnInfo);
  return (short) value;
}
 
Example 6
private Number parseFromBigDecimal(final Class<? extends Number> type, final BigDecimal number) {
    
    if(Byte.class.isAssignableFrom(type) || byte.class.isAssignableFrom(type)) {
        return lenient ? number.byteValue() : number.byteValueExact();
        
    } else if(Short.class.isAssignableFrom(type) || short.class.isAssignableFrom(type)) {
        return lenient ? number.shortValue() : number.shortValueExact();
        
    } else if(Integer.class.isAssignableFrom(type) || int.class.isAssignableFrom(type)) {
        return lenient ? number.intValue() : number.intValueExact();
        
    } else if(Long.class.isAssignableFrom(type) || long.class.isAssignableFrom(type)) {
        return lenient ? number.longValue() : number.longValueExact();
        
    } else if(Float.class.isAssignableFrom(type) || float.class.isAssignableFrom(type)) {
        return number.floatValue();
        
    } else if(Double.class.isAssignableFrom(type) || double.class.isAssignableFrom(type)) {
        return number.doubleValue();
        
    } else if(type.isAssignableFrom(BigInteger.class)) {
        return lenient ? number.toBigInteger() : number.toBigIntegerExact();
        
    } else if(type.isAssignableFrom(BigDecimal.class)) {
        return number;
        
    }
    
    throw new IllegalArgumentException(String.format("Not support class type : %s", type.getCanonicalName()));
    
}
 
Example 7
private Number convertWithBigDecimal(final Class<? extends Number> type, final BigDecimal number, final String str) {
    
    if(Byte.class.isAssignableFrom(type) || byte.class.isAssignableFrom(type)) {
        return lenient ? number.byteValue() : number.byteValueExact();
        
    } else if(Short.class.isAssignableFrom(type) || short.class.isAssignableFrom(type)) {
        return lenient ? number.shortValue() : number.shortValueExact();
        
    } else if(Integer.class.isAssignableFrom(type) || int.class.isAssignableFrom(type)) {
        return lenient ? number.intValue() : number.intValueExact();
        
    } else if(Long.class.isAssignableFrom(type) || long.class.isAssignableFrom(type)) {
        return lenient ? number.longValue() : number.longValueExact();
        
    } else if(Float.class.isAssignableFrom(type) || float.class.isAssignableFrom(type)) {
        return number.floatValue();
        
    } else if(Double.class.isAssignableFrom(type) || double.class.isAssignableFrom(type)) {
        return number.doubleValue();
        
    } else if(type.isAssignableFrom(BigInteger.class)) {
        return lenient ? number.toBigInteger() : number.toBigIntegerExact();
        
    } else if(type.isAssignableFrom(BigDecimal.class)) {
        return number;
        
    }
    
    throw new IllegalArgumentException(String.format("not support class type : %s", type.getCanonicalName()));
    
}