Package com.amazon.ion
Class Decimal
java.lang.Object
java.lang.Number
java.math.BigDecimal
com.amazon.ion.Decimal
- All Implemented Interfaces:
Serializable,Comparable<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:
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) 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 Summary
FieldsModifier and TypeFieldDescriptionstatic final DecimalThe value -0, with a scale of 0.static final DecimalThe value 0, with a scale of 0.Fields inherited from class java.math.BigDecimal
ONE, ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UNNECESSARY, ROUND_UP, TEN, TWO -
Method Summary
Modifier and TypeMethodDescriptionfinal BigDecimalConverts this to a "plain"BigDecimalinstance, losing any negative zeros in the process.static BigDecimalReturns a "plain"BigDecimalinstance, never aDecimalsubclass.static booleanequals(BigDecimal v1, BigDecimal v2) Compares two decimal values for equality, observing both precision and negative zeros.final booleanstatic booleanisNegativeZero(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, 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, MathContext mc) static DecimalvalueOf(int val) static DecimalvalueOf(int val, MathContext mc) static DecimalvalueOf(long val) static DecimalvalueOf(long val, MathContext mc) static Decimalstatic DecimalvalueOf(String val, MathContext mc) static DecimalvalueOf(BigDecimal val) static DecimalvalueOf(BigDecimal val, MathContext mc) static DecimalvalueOf(BigInteger val) static DecimalvalueOf(BigInteger unscaledVal, int scale) static DecimalvalueOf(BigInteger unscaledVal, int scale, MathContext mc) static DecimalvalueOf(BigInteger val, 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, valueOfMethods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ZERO
The value 0, with a scale of 0. -
NEGATIVE_ZERO
The value -0, with a scale of 0.
-
-
Method Details
-
isNegativeZero
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:
NullPointerException- if the value isnull.
-
bigDecimalValue
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
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:
NullPointerException- if either parameter isnull.
-
negativeZero
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
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
-
valueOf
-
valueOf
-
valueOf
-
valueOf
-
valueOf
-
valueOf
-
valueOf
-
valueOf
-
valueOf
-
valueOf
-
valueOf
-
valueOf
-
valueOf
-
isNegativeZero
public final boolean isNegativeZero() -
bigDecimalValue
Converts this to a "plain"BigDecimalinstance, losing any negative zeros in the process.- Returns:
- a
BigDecimal, never aDecimal.
-