/** * The size of this type on the wire * @param typeAttributes the additional attributes of the type. * @return A size */ public int getSize(ColumnTypeAttributes typeAttributes) { if (this == DECIMAL) { return DecimalUtil.precisionToSize(typeAttributes.getPrecision()); } return this.size; }
/** * Creates a new byte array containing a little-endian BigDecimal. * @param n A BigDecimal. * @param precision The target precision of the decimal value. * @return A new byte array containing the given value. */ public static byte[] fromBigDecimal(final BigDecimal n, int precision) { int size = DecimalUtil.precisionToSize(precision); switch (size) { case DecimalUtil.DECIMAL32_SIZE: return fromInt(n.unscaledValue().intValue()); case DecimalUtil.DECIMAL64_SIZE: return fromLong(n.unscaledValue().longValue()); case DecimalUtil.DECIMAL128_SIZE: return fromBigInteger(n.unscaledValue()); default: throw new IllegalArgumentException("Unsupported decimal type size: " + size); } }
/** * Reads a decimal from the beginning of the given array. * @param b The array to read from. * @param offset The offset in the array to start reading from. * @param precision The precision of the decimal value. * @return A BigDecimal. * @throws IndexOutOfBoundsException if the byte array is too small. */ public static BigDecimal getDecimal(final byte[] b, final int offset, int precision, int scale) { int size = DecimalUtil.precisionToSize(precision); switch (size) { case DecimalUtil.DECIMAL32_SIZE: int intVal = getInt(b, offset); return BigDecimal.valueOf(intVal, scale); case DecimalUtil.DECIMAL64_SIZE: long longVal = getLong(b, offset); return BigDecimal.valueOf(longVal, scale); case DecimalUtil.DECIMAL128_SIZE: BigInteger int128Val = getBigInteger(b, offset); return new BigDecimal(int128Val, scale); default: throw new IllegalArgumentException("Unsupported decimal type size: " + size); } }
int size = DecimalUtil.precisionToSize(precision); BigInteger bigInt = n.unscaledValue(); switch (size) {