/** * Returns a {@link LongsBsonDecimal128} instance with the given value. */ public static LongsBsonDecimal128 newInstance(BigDecimal value) { return new LongsBsonDecimal128(value); }
public static BsonDecimal128 newDecimal128(long high, long low) { return LongsBsonDecimal128.newInstance(high, low); }
private LongsBsonDecimal128(BigDecimal value) { bigDecimalToDecimal128(value, value.signum() == -1); }
private BigDecimal getBigDecimal() { int scale = -getExponent(); if (twoHighestCombinationBitsAreSet()) { return BigDecimal.valueOf(0, scale); } return new BigDecimal(new BigInteger(isNegative() ? -1 : 1, getBytes()), scale); }
/** * Get a value of this {@link LongsBsonDecimal128} in an equivalent BigDecimal * * @return BigDecimal equivalent to this Decimal128 */ @Override public BigDecimal getValue() { if (isNaN()) { throw new ArithmeticException("Can not get value of NaN"); } if (isInfinite()) { throw new ArithmeticException("Can not get value of Infinite"); } BigDecimal value = getBigDecimal(); // If the BigDecimal is 0, but the Decimal128 is negative, that means we have -0. if (isNegative() && value.signum() == 0) { throw new ArithmeticException("Negative zero can not be converted to a BigDecimal"); } return value; }
int undiscardedPrecision = ensureExactRounding(initialValue, diff); BigInteger divisor = undiscardedPrecision == 0 ? BIG_INT_ONE : BIG_INT_TEN.pow(diff); value = new BigDecimal(initialValue.unscaledValue().divide(divisor), if (extraPrecision > 0) { ensureExactRounding(initialValue, extraPrecision);
long localLow = 0; BigDecimal value = clampAndRound(initialValue);
/** * Returns a {@link LongsBsonDecimal128} instance with the given value. */ public static LongsBsonDecimal128 newInstance(long high, long low) { return new LongsBsonDecimal128(high, low); }
@Override BsonDecimal128 readDecimal128(@Loose @ModifiesIndexes ByteBuf byteBuf) { return LongsBsonDecimal128.newInstance(new BigDecimal(byteBuf.readDouble())); }
return new LongsBsonDecimal128( value.asDecimal128().decimal128Value().getHigh(), value.asDecimal128().decimal128Value().getLow());