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

The following examples show how to use java.math.BigDecimal#shortValueExact() . 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 File: PSmallint.java    From phoenix with 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 File: PSmallint.java    From phoenix with 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 File: TypeUtils.java    From grpc-nebula-java with 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 File: TypeUtils.java    From uavstack with 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 File: ShortTypeHandler.java    From beanio with Apache License 2.0 4 votes vote down vote up
@Override
protected Short createNumber(BigDecimal bg) throws ArithmeticException {
    return bg.shortValueExact();
}
 
Example 6
Source File: ShortCellConverterFactory.java    From xlsmapper with Apache License 2.0 4 votes vote down vote up
@Override
protected Short convertTypeValue(final BigDecimal value) throws NumberFormatException, ArithmeticException {
    // 少数以下を四捨五入
    BigDecimal decimal = value.setScale(0, RoundingMode.HALF_UP);
    return decimal.shortValueExact();
}
 
Example 7
Source File: SimpleNumberFormatter.java    From super-csv-annotation with Apache License 2.0 3 votes vote down vote up
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
Source File: NumberFormatWrapper.java    From super-csv-annotation with Apache License 2.0 3 votes vote down vote up
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()));
    
}