Package com.amazon.ion
Class Decimal
- java.lang.Object
-
- java.lang.Number
-
- java.math.BigDecimal
-
- com.amazon.ion.Decimal
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<java.math.BigDecimal>
public class Decimal extends java.math.BigDecimalAn extension ofBigDecimalthat can represent negative zeros. The primary change is the addition ofisNegativeZero().WARNING: This class should not be extended by code outside of this library.
This class currently does not have any behavioral difference from
BigDecimalexcept for the following:BigDecimal.toString(),BigDecimal.toEngineeringString(), andBigDecimal.toPlainString()print a negative sign if necessary.BigDecimal.floatValue()andBigDecimal.doubleValue()return negative zero results.BigDecimal.abs()does the right thing.
equals(java.math.BigDecimal, java.math.BigDecimal)orBigDecimal.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:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static DecimalNEGATIVE_ZEROThe value -0, with a scale of 0.static DecimalZEROThe value 0, with a scale of 0.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.math.BigDecimalbigDecimalValue()Converts this to a "plain"BigDecimalinstance, losing any negative zeros in the process.static java.math.BigDecimalbigDecimalValue(java.math.BigDecimal val)Returns a "plain"BigDecimalinstance, never aDecimalsubclass.static booleanequals(java.math.BigDecimal v1, java.math.BigDecimal v2)Compares two decimal values for equality, observing both precision and negative zeros.booleanisNegativeZero()static booleanisNegativeZero(java.math.BigDecimal val)Efficiently determines whether an arbitary decimal value is a negative zero.static DecimalnegativeZero(int scale)Returns a negative-zero decimal value, with the given number of significant digits (zeros).static DecimalnegativeZero(int scale, java.math.MathContext mc)Returns a negative-zero decimal value, with the given number of significant digits (zeros) and given context.static DecimalvalueOf(double val)static DecimalvalueOf(double val, java.math.MathContext mc)static DecimalvalueOf(int val)static DecimalvalueOf(int val, java.math.MathContext mc)static DecimalvalueOf(long val)static DecimalvalueOf(long val, java.math.MathContext mc)static DecimalvalueOf(java.lang.String val)static DecimalvalueOf(java.lang.String val, java.math.MathContext mc)static DecimalvalueOf(java.math.BigDecimal val)static DecimalvalueOf(java.math.BigDecimal val, java.math.MathContext mc)static DecimalvalueOf(java.math.BigInteger val)static DecimalvalueOf(java.math.BigInteger unscaledVal, int scale)static DecimalvalueOf(java.math.BigInteger unscaledVal, int scale, java.math.MathContext mc)static DecimalvalueOf(java.math.BigInteger val, java.math.MathContext mc)-
Methods inherited from class java.math.BigDecimal
abs, abs, add, add, byteValueExact, compareTo, divide, divide, divide, divide, divide, divide, divideAndRemainder, divideAndRemainder, divideToIntegralValue, divideToIntegralValue, doubleValue, equals, floatValue, hashCode, intValue, intValueExact, longValue, longValueExact, max, min, movePointLeft, movePointRight, multiply, multiply, negate, negate, plus, plus, pow, pow, precision, remainder, remainder, round, scale, scaleByPowerOfTen, setScale, setScale, setScale, shortValueExact, signum, sqrt, stripTrailingZeros, subtract, subtract, toBigInteger, toBigIntegerExact, toEngineeringString, toPlainString, toString, ulp, unscaledValue, valueOf
-
-
-
-
Method Detail
-
isNegativeZero
public static boolean isNegativeZero(java.math.BigDecimal val)
Efficiently determines whether an arbitary decimal value is a negative zero. This can only be true when the value is actually aDecimal.- Returns:
trueif and only if the value is a negative zero.- Throws:
java.lang.NullPointerException- if the value isnull.
-
bigDecimalValue
public static java.math.BigDecimal bigDecimalValue(java.math.BigDecimal val)
Returns a "plain"BigDecimalinstance, never aDecimalsubclass. As a side effect, this strips any negative-zero information.- Parameters:
val- may be null.- Returns:
nullif the given value isnull.
-
equals
public static boolean equals(java.math.BigDecimal v1, java.math.BigDecimal v2)Compares two decimal values for equality, observing both precision and negative zeros. This differs fromBigDecimal.equals(Object), which isn't aware of negative zeros, and fromBigDecimal.compareTo(BigDecimal), which ignores both precision and negative zeros.- Returns:
trueif and only if the twoBigDecimalobjects have the same value, scale, and sign.- Throws:
java.lang.NullPointerException- if either parameter isnull.
-
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, java.math.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(java.math.BigInteger unscaledVal, int scale)
-
valueOf
public static Decimal valueOf(java.math.BigInteger unscaledVal, int scale, java.math.MathContext mc)
-
valueOf
public static Decimal valueOf(java.math.BigInteger val)
-
valueOf
public static Decimal valueOf(java.math.BigInteger val, java.math.MathContext mc)
-
valueOf
public static Decimal valueOf(int val)
-
valueOf
public static Decimal valueOf(int val, java.math.MathContext mc)
-
valueOf
public static Decimal valueOf(long val)
-
valueOf
public static Decimal valueOf(long val, java.math.MathContext mc)
-
valueOf
public static Decimal valueOf(double val)
-
valueOf
public static Decimal valueOf(double val, java.math.MathContext mc)
-
valueOf
public static Decimal valueOf(java.math.BigDecimal val)
-
valueOf
public static Decimal valueOf(java.math.BigDecimal val, java.math.MathContext mc)
-
valueOf
public static Decimal valueOf(java.lang.String val)
-
valueOf
public static Decimal valueOf(java.lang.String val, java.math.MathContext mc)
-
isNegativeZero
public final boolean isNegativeZero()
-
bigDecimalValue
public final java.math.BigDecimal bigDecimalValue()
Converts this to a "plain"BigDecimalinstance, losing any negative zeros in the process.- Returns:
- a
BigDecimal, never aDecimal.
-
-