Class Decimal

All Implemented Interfaces:
Serializable, Comparable<BigDecimal>

public class Decimal extends BigDecimal
An extension of BigDecimal that can represent negative zeros. The primary change is the addition of isNegativeZero().

WARNING: This class should not be extended by code outside of this library.

This class currently does not have any behavioral difference from BigDecimal except for the following:

This class does NOT override equals(java.math.BigDecimal, java.math.BigDecimal) or BigDecimal.compareTo(java.math.BigDecimal) so those methods cannot distinguish positive and negative zeros.

It also does not currently override any of the numerical methods, but it may do so in the future. If you are concerned about consistent treatment of negative zeros in future releases, you may wish to use bigDecimalValue(java.math.BigDecimal) before performing those operations.

See Also:
  • Field Details

    • ZERO

      public static final Decimal ZERO
      The value 0, with a scale of 0.
    • NEGATIVE_ZERO

      public static final Decimal NEGATIVE_ZERO
      The value -0, with a scale of 0.
  • Method Details

    • isNegativeZero

      public static boolean isNegativeZero(BigDecimal val)
      Efficiently determines whether an arbitary decimal value is a negative zero. This can only be true when the value is actually a Decimal.
      Returns:
      true if and only if the value is a negative zero.
      Throws:
      NullPointerException - if the value is null.
    • bigDecimalValue

      public static BigDecimal bigDecimalValue(BigDecimal val)
      Returns a "plain" BigDecimal instance, never a Decimal subclass. As a side effect, this strips any negative-zero information.
      Parameters:
      val - may be null.
      Returns:
      null if the given value is null.
    • equals

      public static boolean equals(BigDecimal v1, BigDecimal v2)
      Compares two decimal values for equality, observing both precision and negative zeros. This differs from BigDecimal.equals(Object), which isn't aware of negative zeros, and from BigDecimal.compareTo(BigDecimal), which ignores both precision and negative zeros.
      Returns:
      true if and only if the two BigDecimal objects have the same value, scale, and sign.
      Throws:
      NullPointerException - if either parameter is null.
    • negativeZero

      public static Decimal negativeZero(int scale)
      Returns a negative-zero decimal value, with the given number of significant digits (zeros).
      Parameters:
      scale - the number of significant digits (zeros) after the decimal point.
    • negativeZero

      public static Decimal negativeZero(int scale, MathContext mc)
      Returns a negative-zero decimal value, with the given number of significant digits (zeros) and given context.
      Parameters:
      scale - the number of significant digits (zeros) after the decimal point.
    • valueOf

      public static Decimal valueOf(BigInteger unscaledVal, int scale)
    • valueOf

      public static Decimal valueOf(BigInteger unscaledVal, int scale, MathContext mc)
    • valueOf

      public static Decimal valueOf(BigInteger val)
    • valueOf

      public static Decimal valueOf(BigInteger val, MathContext mc)
    • valueOf

      public static Decimal valueOf(int val)
    • valueOf

      public static Decimal valueOf(int val, MathContext mc)
    • valueOf

      public static Decimal valueOf(long val)
    • valueOf

      public static Decimal valueOf(long val, MathContext mc)
    • valueOf

      public static Decimal valueOf(double val)
    • valueOf

      public static Decimal valueOf(double val, MathContext mc)
    • valueOf

      public static Decimal valueOf(BigDecimal val)
    • valueOf

      public static Decimal valueOf(BigDecimal val, MathContext mc)
    • valueOf

      public static Decimal valueOf(String val)
    • valueOf

      public static Decimal valueOf(String val, MathContext mc)
    • isNegativeZero

      public final boolean isNegativeZero()
    • bigDecimalValue

      public final BigDecimal bigDecimalValue()
      Converts this to a "plain" BigDecimal instance, losing any negative zeros in the process.
      Returns:
      a BigDecimal, never a Decimal.