Refine search
public static SqlDecimal of(String decimalValue) { BigDecimal bigDecimal = new BigDecimal(decimalValue); return new SqlDecimal(bigDecimal.unscaledValue(), bigDecimal.precision(), bigDecimal.scale()); }
public static Decimal toMetastoreDecimal(BigDecimal decimal) { return new Decimal(ByteBuffer.wrap(decimal.unscaledValue().toByteArray()), (short) decimal.scale()); }
private byte[] convertFromDecimal(Schema schema, BigDecimal decimal) { final LogicalType logicalType = schema.getLogicalType(); if (logicalType instanceof LogicalTypes.Decimal) { final LogicalTypes.Decimal decimalType = (LogicalTypes.Decimal) logicalType; // rescale to target type final BigDecimal rescaled = decimal.setScale(decimalType.getScale(), BigDecimal.ROUND_UNNECESSARY); // byte array must contain the two's-complement representation of the // unscaled integer value in big-endian byte order return decimal.unscaledValue().toByteArray(); } else { throw new RuntimeException("Unsupported decimal type."); } }
public static Decimal createThriftDecimal(String s) { BigDecimal d = new BigDecimal(s); return new Decimal((short) d.scale(), ByteBuffer.wrap(d.unscaledValue().toByteArray())); }
public void setSafe(int index, BigDecimal value) { byte[] bytes = value.unscaledValue().toByteArray(); setSafe(index, bytes, 0, bytes.length); }
public static SqlDecimal of(String decimalValue) { BigDecimal bigDecimal = new BigDecimal(decimalValue); return new SqlDecimal(bigDecimal.unscaledValue(), bigDecimal.precision(), bigDecimal.scale()); }
public StatisticsHasher putOptionalBigDecimal(BigDecimal value) { hasher.putBoolean(value != null); if (value != null) { // this should really be 128 bits hasher.putInt(value.scale()); hasher.putBytes(value.unscaledValue().toByteArray()); } return this; }
public void set(int index, BigDecimal value) { byte[] bytes = value.unscaledValue().toByteArray(); set(index, bytes, 0, bytes.length); }
/** * Updates the value of this object with the given {@link BigDecimal}. * @param bigDecimal * {@link java.math.BigDecimal} */ public Decimal128 update(BigDecimal bigDecimal) { return update(bigDecimal.unscaledValue(), (short) bigDecimal.scale()); }
/** * Convert a BigDecimal value to a byte array * * @param val * @return the byte array */ public static byte[] toBytes(BigDecimal val) { byte[] valueBytes = val.unscaledValue().toByteArray(); byte[] result = new byte[valueBytes.length + SIZEOF_INT]; int offset = putInt(result, 0, val.scale()); putBytes(result, offset, valueBytes, 0, valueBytes.length); return result; }
DateTime.parse("2014-03-01T12:12:12.321Z"), 123456L, ByteBuffer.wrap(BigDecimal.valueOf(2000, 2).unscaledValue().toByteArray()), // 20.00 new Fixed2(BigDecimal.valueOf(2000, 2).unscaledValue().toByteArray())); // 20.00
int scale = v.scale(); int expOffset = scale % 2 * (scale < 0 ? -1 : 1); int multiplyBy; BigInteger bi = v.unscaledValue();
/** * Convert a BigDecimal value to a byte array * * @param val * @return the byte array */ public static byte[] toBytes(BigDecimal val) { byte[] valueBytes = val.unscaledValue().toByteArray(); byte[] result = new byte[valueBytes.length + SIZEOF_INT]; int offset = putInt(result, 0, val.scale()); putBytes(result, offset, valueBytes, 0, valueBytes.length); return result; }
public static User generateRandomUser(Random rnd) { return new User( generateRandomString(rnd, 50), rnd.nextBoolean() ? null : rnd.nextInt(), rnd.nextBoolean() ? null : generateRandomString(rnd, 6), rnd.nextBoolean() ? null : rnd.nextLong(), rnd.nextDouble(), null, rnd.nextBoolean(), generateRandomStringList(rnd, 20, 30), generateRandomBooleanList(rnd, 20), rnd.nextBoolean() ? null : generateRandomStringList(rnd, 20, 20), generateRandomColor(rnd), new HashMap<>(), generateRandomFixed16(rnd), generateRandomUnion(rnd), generateRandomAddress(rnd), generateRandomBytes(rnd), LocalDate.parse("2014-03-01"), LocalTime.parse("12:12:12"), 123456, DateTime.parse("2014-03-01T12:12:12.321Z"), 123456L, ByteBuffer.wrap(BigDecimal.valueOf(2000, 2).unscaledValue().toByteArray()), new Fixed2(BigDecimal.valueOf(2000, 2).unscaledValue().toByteArray())); }
String intString = bd.unscaledValue().toString(); int scale = bd.scale();
@Override public ByteBuffer toBytes(BigDecimal value, Schema schema, LogicalType type) { int scale = ((LogicalTypes.Decimal) type).getScale(); if (scale != value.scale()) { throw new AvroTypeException("Cannot encode decimal with scale " + value.scale() + " as scale " + scale); } return ByteBuffer.wrap(value.unscaledValue().toByteArray()); }
@Test public void testCopyDecimalRaw() { testCopy(LogicalTypes.decimal(9, 2).addToSchema(Schema.create(Schema.Type.BYTES)), ByteBuffer.wrap(new BigDecimal("-34.34").unscaledValue().toByteArray()), GenericData.get()); // no conversions }
this.to = to; this.incrementUnit = incrementUnit; int scale = from.scale(); if (scale != to.scale()) { throw new IllegalArgumentException("The " + getClass().getSimpleName() + " cannot have a to (" + to + ") scale (" + to.scale() + ") which is different than its from (" + from + ") scale (" + scale + ")."); if (!to.unscaledValue().subtract(from.unscaledValue()).remainder(incrementUnit.unscaledValue()) .equals(BigInteger.ZERO)) { throw new IllegalArgumentException("The " + getClass().getSimpleName()
@Override public ByteBuffer toBytes(BigDecimal value, Schema schema, LogicalType type) { int scale = ((LogicalTypes.Decimal) type).getScale(); if (scale != value.scale()) { throw new AvroTypeException("Cannot encode decimal with scale " + value.scale() + " as scale " + scale); } return ByteBuffer.wrap(value.unscaledValue().toByteArray()); }
@Override public User map(Tuple3<String, Integer, String> value) { User user = new User(); user.setName(value.f0); user.setFavoriteNumber(value.f1); user.setFavoriteColor(value.f2); user.setTypeBoolTest(true); user.setTypeArrayString(Collections.emptyList()); user.setTypeArrayBoolean(Collections.emptyList()); user.setTypeEnum(Colors.BLUE); user.setTypeMap(Collections.emptyMap()); user.setTypeBytes(ByteBuffer.allocate(10)); user.setTypeDate(LocalDate.parse("2014-03-01")); user.setTypeTimeMillis(LocalTime.parse("12:12:12")); user.setTypeTimeMicros(123456); user.setTypeTimestampMillis(DateTime.parse("2014-03-01T12:12:12.321Z")); user.setTypeTimestampMicros(123456L); // 20.00 user.setTypeDecimalBytes(ByteBuffer.wrap(BigDecimal.valueOf(2000, 2).unscaledValue().toByteArray())); // 20.00 user.setTypeDecimalFixed(new Fixed2(BigDecimal.valueOf(2000, 2).unscaledValue().toByteArray())); return user; } }