private boolean hasDifferentSign(final BigDecimal bigDecimal) { return isNegative() && bigDecimal.signum() == 0; }
private BigDecimal bigDecimalValueNoNegativeZeroCheck() { int scale = -getExponent(); if (twoHighestCombinationBitsAreSet()) { return BigDecimal.valueOf(0, scale); } return new BigDecimal(new BigInteger(isNegative() ? -1 : 1, getBytes()), scale); }
String significand = bigDecimal.unscaledValue().abs().toString(); if (isNegative()) { buffer.append('-');
/** * Gets a BigDecimal that is equivalent to this Decimal128. * * @return a BigDecimal that is equivalent to this Decimal128 * @throws ArithmeticException if the Decimal128 value is NaN, Infinity, -Infinity, or -0, none of which can be represented as a * BigDecimal */ public BigDecimal bigDecimalValue() { if (isNaN()) { throw new ArithmeticException("NaN can not be converted to a BigDecimal"); } if (isInfinite()) { throw new ArithmeticException("Infinity can not be converted to a BigDecimal"); } BigDecimal bigDecimal = bigDecimalValueNoNegativeZeroCheck(); // If the BigDecimal is 0, but the Decimal128 is negative, that means we have -0. if (isNegative() && bigDecimal.signum() == 0) { throw new ArithmeticException("Negative zero can not be converted to a BigDecimal"); } return bigDecimal; }
/** * Returns the String representation of the Decimal128 value. * * @return the String representation * @see <a href="https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst#to-string-representation"> * To-String Sprecification</a> */ @Override public String toString() { if (isNaN()) { return "NaN"; } if (isInfinite()) { if (isNegative()) { return "-Infinity"; } else { return "Infinity"; } } return toStringWithBigDecimal(); }
/** * Converts this {@code Decimal128} to a {@code double}. This conversion is similar to the <i>narrowing primitive conversion</i> from * {@code double} to {@code float} as defined in <cite>The Java™ Language Specification</cite>: if this {@code Decimal128} has * too great a magnitude to represent as a {@code double}, it will be converted to {@link Double#NEGATIVE_INFINITY} or * {@link Double#POSITIVE_INFINITY} as appropriate. Note that even when the return value is finite, this conversion can lose * information about the precision of the {@code Decimal128} value. Note that {@code #NEGATIVE_ZERO} is converted to {@code 0.0d}. * * @return this {@code Decimal128} converted to a {@code double}. * @since 3.10 */ @Override public double doubleValue() { if (isNaN()) { return Double.NaN; } if (isInfinite()) { if (isNegative()) { return Double.NEGATIVE_INFINITY; } else { return Double.POSITIVE_INFINITY; } } BigDecimal bigDecimal = bigDecimalValueNoNegativeZeroCheck(); if (hasDifferentSign(bigDecimal)) { return -0.0d; } return bigDecimal.doubleValue(); }
if (isNegative()) { if (o.isInfinite() && o.isNegative()) { return 0; } else { if (o.isNaN()) { return -1; } else if (o.isInfinite() && !o.isNegative()) { return 0; } else { return -1; } else if (o.isInfinite()) { if (o.isNegative()) { return 1; } else {