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

The following examples show how to use java.math.BigDecimal#byteValueExact() . 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: PTinyint.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public Byte toObject(byte[] b, int o, int l, PDataType actualType, SortOrder sortOrder,
    Integer maxLength, Integer scale) {
  if (l == 0) {
    return null;
  }
  if (equalsAny(actualType, PDouble.INSTANCE, PUnsignedDouble.INSTANCE, PFloat.INSTANCE,
      PUnsignedFloat.INSTANCE, PLong.INSTANCE, PUnsignedLong.INSTANCE, PInteger.INSTANCE,
      PUnsignedInt.INSTANCE, PSmallint.INSTANCE, PUnsignedSmallint.INSTANCE, PTinyint.INSTANCE,
      PUnsignedTinyint.INSTANCE)) {
    return actualType.getCodec().decodeByte(b, o, sortOrder);
  } else if (actualType == PDecimal.INSTANCE) {
    BigDecimal bd = (BigDecimal) actualType.toObject(b, o, l, actualType, sortOrder);
    return bd.byteValueExact();
  }
  throwConstraintViolationException(actualType, this);
  return null;
}
 
Example 2
Source File: PTinyint.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public Byte toObject(byte[] b, int o, int l, PDataType actualType, SortOrder sortOrder,
    Integer maxLength, Integer scale) {
  if (l == 0) {
    return null;
  }
  if (equalsAny(actualType, PDouble.INSTANCE, PUnsignedDouble.INSTANCE, PFloat.INSTANCE,
      PUnsignedFloat.INSTANCE, PLong.INSTANCE, PUnsignedLong.INSTANCE, PInteger.INSTANCE,
      PUnsignedInt.INSTANCE, PSmallint.INSTANCE, PUnsignedSmallint.INSTANCE, PTinyint.INSTANCE,
      PUnsignedTinyint.INSTANCE)) {
    return actualType.getCodec().decodeByte(b, o, sortOrder);
  } else if (actualType == PDecimal.INSTANCE) {
    BigDecimal bd = (BigDecimal) actualType.toObject(b, o, l, actualType, sortOrder);
    return bd.byteValueExact();
  }
  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 byte byteValue(BigDecimal decimal) {
    if (decimal == null) {
        return 0;
    }

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

    return decimal.byteValueExact();
}
 
Example 4
Source File: TypeUtils.java    From uavstack with Apache License 2.0 5 votes vote down vote up
public static byte byteValue(BigDecimal decimal) {
    if (decimal == null) {
        return 0;
    }

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

    return decimal.byteValueExact();
}
 
Example 5
Source File: BigDecimalTest.java    From nuls with MIT License 4 votes vote down vote up
@View
    public String cal1() {
        /**
         * 构造函数测试
         */
        BigDecimal _int = new BigDecimal(19);
        BigDecimal _double = new BigDecimal(4.56D);
        BigDecimal _long = new BigDecimal(5634345L);
        BigDecimal _String = new BigDecimal("1000022");

        /**
         * 基本方法
         */
        BigDecimal amount = BigDecimal.TEN;
        BigDecimal fee = BigDecimal.ONE;

        BigDecimal add = amount.add(fee);
        BigDecimal subtract = amount.subtract(fee);
        BigDecimal multiply = amount.multiply(fee);

        BigDecimal divisor = new BigDecimal("4.5678");
        /**
         * 保留3位小数,且四舍五入
         * ROUND_CEILING  向正无穷方向舍入
         * ROUND_DOWN   向零方向舍入
         * ROUND_FLOOR   向负无穷方向舍入
         * ROUND_HALF_DOWN  向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向下舍入, 例如1.55 保留一位小数结果为1.5
         * ROUND_HALF_EVEN  向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,如果保留位数是奇数,使用ROUND_HALF_UP,如果是偶数,使用ROUND_HALF_DOWN
         * ROUND_HALF_UP    向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向上舍入, 1.55保留一位小数结果为1.6
         * ROUND_UNNECESSARY  计算结果是精确的,不需要舍入模式
         * ROUND_UP 向远离0的方向舍入
         */
        BigDecimal result = amount.divide(divisor, 3, BigDecimal.ROUND_HALF_UP);

        /**
         * 普通方法
         */
        String _toString = amount.toString();
        Double _doubleValue = amount.doubleValue();
        Float _floatValue = amount.floatValue();
        Long _longValue = amount.longValue();
        Integer _intValue = amount.intValue();

        StringBuilder sb = new StringBuilder();
        /**
         * 科学计数法问题
         */
        BigDecimal hex = new BigDecimal("192320012000000000000000");
        hex.byteValueExact();
//        hex.plus();
        BigDecimal pow = hex.pow(6);

        sb.append("\npow: " + pow.toString() + " ==== " + pow.toPlainString());
        String hex2 = hex.toString();
        String plain = hex.toPlainString();

        sb.append("\nmovePointRight9: " + pow.movePointRight(9));
        sb.append("\nmovePointLeft9: " + pow.movePointLeft(9));
        sb.append("\nsetScale3: " + divisor.setScale(3));
        sb.append("\nsetScale3_ROUND_HALF_DOWN: " + divisor.setScale(3, BigDecimal.ROUND_HALF_DOWN));
        sb.append("\nsetScale3_ROUND_HALF_DOWN: " + divisor);


        return sb.toString();
    }
 
Example 6
Source File: ByteCellConverterFactory.java    From xlsmapper with Apache License 2.0 4 votes vote down vote up
@Override
protected Byte convertTypeValue(final BigDecimal value) throws NumberFormatException, ArithmeticException {
    // 少数以下を四捨五入
    BigDecimal decimal = value.setScale(0, RoundingMode.HALF_UP);
    return decimal.byteValueExact();
}
 
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()));
    
}