private int numberOfDecimals(String value) { return new BigDecimal(value).stripTrailingZeros().scale(); }
public static Decimal createThriftDecimal(String s) { BigDecimal d = new BigDecimal(s); return new Decimal((short) d.scale(), ByteBuffer.wrap(d.unscaledValue().toByteArray())); }
public static long parseMicroCost(String costString) { BigDecimal costBigDecimal = new BigDecimal(costString); if (costBigDecimal.scale() > 11) { throw new IllegalArgumentException("The costString (" + costString + ") has a scale (" + costBigDecimal.scale() + ") higher than 10."); } costBigDecimal = costBigDecimal.setScale(11); return costBigDecimal.multiply(MICROS_PER_ONE_AS_BIG_DECIMAL).longValueExact(); }
@Override public HardMediumSoftBigDecimalScore multiply(double multiplicand) { // Intentionally not taken "new BigDecimal(multiplicand, MathContext.UNLIMITED)" // because together with the floor rounding it gives unwanted behaviour BigDecimal multiplicandBigDecimal = BigDecimal.valueOf(multiplicand); // The (unspecified) scale/precision of the multiplicand should have no impact on the returned scale/precision return new HardMediumSoftBigDecimalScore( (int) Math.floor(initScore * multiplicand), hardScore.multiply(multiplicandBigDecimal).setScale(hardScore.scale(), RoundingMode.FLOOR), mediumScore.multiply(multiplicandBigDecimal).setScale(mediumScore.scale(), RoundingMode.FLOOR), softScore.multiply(multiplicandBigDecimal).setScale(softScore.scale(), RoundingMode.FLOOR)); }
public static SqlDecimal of(String decimalValue) { BigDecimal bigDecimal = new BigDecimal(decimalValue); return new SqlDecimal(bigDecimal.unscaledValue(), bigDecimal.precision(), bigDecimal.scale()); }
private BigDecimal parseBigDecimal(Slice slice, int offset, int length) { checkArgument(length < buffer.length); for (int i = 0; i < length; i++) { buffer[i] = (char) slice.getByte(offset + i); } BigDecimal decimal = new BigDecimal(buffer, 0, length); checkState(decimal.scale() <= type.getScale(), "Read decimal value scale larger than column scale"); decimal = decimal.setScale(type.getScale(), HALF_UP); checkState(decimal.precision() <= type.getPrecision(), "Read decimal precision larger than column precision"); return decimal; } }
private static Number unwrapNumber(final Number n) { Number unwrapped; if (!isPrimitiveNumber(n)) { BigDecimal bigDecimal = new BigDecimal(n.toString()); if (bigDecimal.scale() <= 0) { if (bigDecimal.compareTo(new BigDecimal(Integer.MAX_VALUE)) <= 0) { unwrapped = bigDecimal.intValue(); } else if (bigDecimal.compareTo(new BigDecimal(Long.MAX_VALUE)) <= 0){ unwrapped = bigDecimal.longValue(); } else { unwrapped = bigDecimal; } } else { final double doubleValue = bigDecimal.doubleValue(); if (BigDecimal.valueOf(doubleValue).compareTo(bigDecimal) != 0) { unwrapped = bigDecimal; } else { unwrapped = doubleValue; } } } else { unwrapped = n; } return unwrapped; }
if (nanoseconds.precision() - nanoseconds.scale() <= 0) { else if (seconds.scale() < -63) { nanosOnly = nanoseconds.subtract(new BigDecimal(secondsOnly).scaleByPowerOfTen(9)).intValue();
BigInteger unscaled = bd.unscaledValue(); int precision = bd.unscaledValue().abs().toString().length(); int scale = bd.scale(); RelDataType relType; case INTERVAL_YEAR_MONTH: BigDecimal totalMonths = BigDecimal.valueOf(((HiveIntervalYearMonth) value).getTotalMonths()); calciteLiteral = rexBuilder.makeIntervalLiteral(totalMonths, new SqlIntervalQualifier(TimeUnit.YEAR, TimeUnit.MONTH, new SqlParserPos(1,1))); .valueOf(((HiveIntervalDayTime) value).getTotalSeconds() * 1000); BigDecimal nanosValueBd = BigDecimal.valueOf(((HiveIntervalDayTime) value).getNanos(), 6); calciteLiteral =
public static Decimal toMetastoreDecimal(BigDecimal decimal) { return new Decimal(ByteBuffer.wrap(decimal.unscaledValue().toByteArray()), (short) decimal.scale()); }
@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } BigDecimal avg = BigDecimal.ZERO; for (int i = start; i < start + length; i++) { avg = avg.add(getBigDecimalForCounter(i)); } avg = avg.divide(BigDecimal.valueOf(length), ROUND_HALF_UP); return new SqlDecimal(avg.unscaledValue(), avg.precision(), avg.scale()); }
@Override public void serialize(BigDecimal value, ByteBuffer out) { if (value == null) { BytesUtil.writeVInt(0, out); BytesUtil.writeVInt(-1, out); return; } if (value.scale() > type.getScale()) { if (avoidVerbose++ % 10000 == 0) { logger.warn("value's scale has exceeded the " + type.getScale() + ", cut it off, to ensure encoded value do not exceed maxLength " + maxLength + " times:" + (avoidVerbose)); } value = value.setScale(type.getScale(), BigDecimal.ROUND_HALF_EVEN); } byte[] bytes = value.unscaledValue().toByteArray(); if (bytes.length + 2 > maxLength) { throw new IllegalArgumentException("'" + value + "' exceeds the expected length for type " + type); } BytesUtil.writeVInt(value.scale(), out); BytesUtil.writeVInt(bytes.length, out); out.put(bytes); }
public static VarDecimalHolder getVarDecimalHolder(DrillBuf buf, BigDecimal bigDecimal) { VarDecimalHolder dch = new VarDecimalHolder(); byte[] bytes = bigDecimal.unscaledValue().toByteArray(); int length = bytes.length; dch.scale = bigDecimal.scale(); dch.precision = bigDecimal.precision(); dch.start = 0; dch.end = length; dch.buffer = buf.reallocIfNeeded(length); dch.buffer.setBytes(0, bytes); return dch; } }
static Number computeValue(final String value) { final BigDecimal bigDecimalValue = new BigDecimal(value); if (bigDecimalValue.scale() > 0) { return bigDecimalValue; } return bigDecimalValue.toBigInteger(); }
@Override public Value divide(Value v) { ValueDecimal dec = (ValueDecimal) v; if (dec.value.signum() == 0) { throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL()); } BigDecimal bd = value.divide(dec.value, value.scale() + DIVIDE_SCALE_ADD, BigDecimal.ROUND_HALF_DOWN); if (bd.signum() == 0) { bd = BigDecimal.ZERO; } else if (bd.scale() > 0) { if (!bd.unscaledValue().testBit(0)) { String s = bd.toString(); int i = s.length() - 1; while (i >= 0 && s.charAt(i) == '0') { i--; } if (i < s.length() - 1) { s = s.substring(0, i + 1); bd = new BigDecimal(s); } } } return ValueDecimal.get(bd); }
public static BigDecimal parseHiveDecimal(byte[] bytes, int start, int length, DecimalType columnType) { BigDecimal parsed = new BigDecimal(new String(bytes, start, length, UTF_8)); if (parsed.scale() > columnType.getScale()) { // Hive rounds HALF_UP too parsed = parsed.setScale(columnType.getScale(), HALF_UP); } return rescale(parsed, columnType); } }