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

The following examples show how to use java.math.BigDecimal#shortValueExact() . 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: phoenix   File: PSmallint.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Short toObject(byte[] b, int o, int l, PDataType actualType, SortOrder sortOrder, Integer maxLength, Integer scale) {
  if (l == 0) {
    return null;
  }
  if (equalsAny(actualType, PSmallint.INSTANCE, PUnsignedSmallint.INSTANCE, PTinyint.INSTANCE,
      PUnsignedTinyint.INSTANCE, PLong.INSTANCE, PUnsignedLong.INSTANCE, PInteger.INSTANCE,
      PUnsignedInt.INSTANCE, PFloat.INSTANCE, PUnsignedFloat.INSTANCE, PDouble.INSTANCE,
      PUnsignedDouble.INSTANCE)) {
    return actualType.getCodec().decodeShort(b, o, sortOrder);
  } else if (actualType == PDecimal.INSTANCE) {
    BigDecimal bd = (BigDecimal)actualType.toObject(b, o, l, actualType, sortOrder);
    return bd.shortValueExact();
  }
  throwConstraintViolationException(actualType,this);
  return null;
}
 
Example 2
Source Project: phoenix   File: PSmallint.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Short toObject(byte[] b, int o, int l, PDataType actualType, SortOrder sortOrder, Integer maxLength, Integer scale) {
  if (l == 0) {
    return null;
  }
  if (equalsAny(actualType, PSmallint.INSTANCE, PUnsignedSmallint.INSTANCE, PTinyint.INSTANCE,
      PUnsignedTinyint.INSTANCE, PLong.INSTANCE, PUnsignedLong.INSTANCE, PInteger.INSTANCE,
      PUnsignedInt.INSTANCE, PFloat.INSTANCE, PUnsignedFloat.INSTANCE, PDouble.INSTANCE,
      PUnsignedDouble.INSTANCE)) {
    return actualType.getCodec().decodeShort(b, o, sortOrder);
  } else if (actualType == PDecimal.INSTANCE) {
    BigDecimal bd = (BigDecimal)actualType.toObject(b, o, l, actualType, sortOrder);
    return bd.shortValueExact();
  }
  throwConstraintViolationException(actualType,this);
  return null;
}
 
Example 3
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 4
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 5
Source Project: beanio   File: ShortTypeHandler.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected Short createNumber(BigDecimal bg) throws ArithmeticException {
    return bg.shortValueExact();
}
 
Example 6
@Override
protected Short convertTypeValue(final BigDecimal value) throws NumberFormatException, ArithmeticException {
    // 少数以下を四捨五入
    BigDecimal decimal = value.setScale(0, RoundingMode.HALF_UP);
    return decimal.shortValueExact();
}
 
Example 7
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 8
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()));
    
}