Top Description Inners Fields Constructors Methods
com.amazon.ion

public Class Decimal

extends BigDecimal
Class Inheritance
Known Direct Subclasses
com.amazon.ion.Decimal.NegativeZero
Imports
java.math.BigDecimal, .BigInteger, .MathContext

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 or compareTo 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 before performing those operations.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
public static final Decimal
NEGATIVE_ZERO

The value -0, with a scale of 0.

private static final long
public static final Decimal
ZERO

Hides java.math.BigDecimal.ZERO.

The value 0, with a scale of 0.
Inherited from java.math.BigDecimal:
ONEROUND_CEILINGROUND_DOWNROUND_FLOORROUND_HALF_DOWNROUND_HALF_EVENROUND_HALF_UPROUND_UNNECESSARYROUND_UPTENTWO

Constructor Summary

AccessConstructor and Description
private
Decimal(BigInteger unscaledVal, int scale)

private
Decimal(BigInteger unscaledVal, int scale, MathContext mc)

private
private
private
Decimal(int val)

private
Decimal(int val, MathContext mc)

private
Decimal(long val)

private
Decimal(long val, MathContext mc)

private
Decimal(double val)

private
Decimal(double val, MathContext mc)

private
Decimal(char[] in, int offset, int len)

private
Decimal(char[] in, int offset, int len, MathContext mc)

private
Decimal(char[] in)

private
Decimal(char[] in, MathContext mc)

private
private

Method Summary

Modifier and TypeMethod and Description
public static BigDecimal

Returns:

null if the given value is null.
bigDecimalValue
(BigDecimal
may be null.
val
)

Returns a "plain" BigDecimal instance, never a Decimal subclass.

public final BigDecimal

Returns:

a BigDecimal, never a Decimal.
bigDecimalValue
()

Converts this to a "plain" BigDecimal instance, losing any negative zeros in the process.

public static boolean

Returns:

true if and only if the two BigDecimal objects have the same value, scale, and sign.
equals
(BigDecimal v1, BigDecimal v2)

Compares two decimal values for equality, observing both precision and negative zeros.

public static boolean

Returns:

true if and only if the value is a negative zero.
isNegativeZero
(BigDecimal val)

Efficiently determines whether an arbitary decimal value is a negative zero.

public final boolean
public static Decimal
negativeZero(int
the number of significant digits (zeros) after the decimal point.
scale
)

Returns a negative-zero decimal value, with the given number of significant digits (zeros).

public static Decimal
negativeZero(int
the number of significant digits (zeros) after the decimal point.
scale
,
MathContext mc)

Returns a negative-zero decimal value, with the given number of significant digits (zeros) and given context.

public static Decimal
valueOf(BigInteger unscaledVal, int scale)

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

public static Decimal
public static Decimal
public static Decimal
valueOf(int val)

public static Decimal
valueOf(int val, MathContext mc)

public static Decimal
valueOf(long
value of the BigDecimal.
val
)

Hides java.math.BigDecimal.valueOf.

Translates a long value into a BigDecimal with a scale of zero.

public static Decimal
valueOf(long val, MathContext mc)

public static Decimal
valueOf(double
double to convert to a BigDecimal.
val
)

Hides java.math.BigDecimal.valueOf.

Translates a double into a BigDecimal, using the double's canonical string representation provided by the Double#toString(double) method.

public static Decimal
valueOf(double val, MathContext mc)

public static Decimal
public static Decimal
public static Decimal
public static Decimal
Inherited from java.math.BigDecimal:
absabsaddaddbyteValueExactcompareTodividedividedividedividedividedividedivideAndRemainderdivideAndRemainderdivideToIntegralValuedivideToIntegralValuedoubleValueequalsfloatValuehashCodeintValueintValueExactlongValuelongValueExactmaxminmovePointLeftmovePointRightmultiplymultiplynegatenegatepluspluspowpowprecisionremainderremainderroundscalescaleByPowerOfTensetScalesetScalesetScaleshortValueExactsignumsqrtstripTrailingZerossubtractsubtracttoBigIntegertoBigIntegerExacttoEngineeringStringtoPlainStringtoStringulpunscaledValuevalueOf

Field Detail

NEGATIVE_ZEROback to summary
public static final Decimal NEGATIVE_ZERO

The value -0, with a scale of 0.

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.math.BigDecimal.serialVersionUID.

ZEROback to summary
public static final Decimal ZERO

Hides java.math.BigDecimal.ZERO.

The value 0, with a scale of 0.

Constructor Detail

Decimalback to summary
private Decimal(BigInteger unscaledVal, int scale)
Decimalback to summary
private Decimal(BigInteger unscaledVal, int scale, MathContext mc)
Decimalback to summary
private Decimal(BigInteger val)
Decimalback to summary
private Decimal(BigInteger val, MathContext mc)
Decimalback to summary
private Decimal(int val)
Decimalback to summary
private Decimal(int val, MathContext mc)
Decimalback to summary
private Decimal(long val)
Decimalback to summary
private Decimal(long val, MathContext mc)
Decimalback to summary
private Decimal(double val)
Decimalback to summary
private Decimal(double val, MathContext mc)
Decimalback to summary
private Decimal(char[] in, int offset, int len)
Decimalback to summary
private Decimal(char[] in, int offset, int len, MathContext mc)
Decimalback to summary
private Decimal(char[] in)
Decimalback to summary
private Decimal(char[] in, MathContext mc)
Decimalback to summary
private Decimal(String val)
Decimalback to summary
private Decimal(String val, MathContext mc)

Method Detail

bigDecimalValueback to summary
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:BigDecimal

may be null.

Returns:BigDecimal

null if the given value is null.

bigDecimalValueback to summary
public final BigDecimal bigDecimalValue()

Converts this to a "plain" BigDecimal instance, losing any negative zeros in the process.

Returns:BigDecimal

a BigDecimal, never a Decimal.

equalsback to summary
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:boolean

true if and only if the two BigDecimal objects have the same value, scale, and sign.

Exceptions
NullPointerException:
if either parameter is null.
isNegativeZeroback to summary
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:boolean

true if and only if the value is a negative zero.

Exceptions
NullPointerException:
if the value is null.
isNegativeZeroback to summary
public final boolean isNegativeZero()
negativeZeroback to summary
public static Decimal negativeZero(int scale)

Returns a negative-zero decimal value, with the given number of significant digits (zeros).

Parameters
scale:int

the number of significant digits (zeros) after the decimal point.

negativeZeroback to summary
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:int

the number of significant digits (zeros) after the decimal point.

valueOfback to summary
public static Decimal valueOf(BigInteger unscaledVal, int scale)
valueOfback to summary
public static Decimal valueOf(BigInteger unscaledVal, int scale, MathContext mc)
valueOfback to summary
public static Decimal valueOf(BigInteger val)
valueOfback to summary
public static Decimal valueOf(BigInteger val, MathContext mc)
valueOfback to summary
public static Decimal valueOf(int val)
valueOfback to summary
public static Decimal valueOf(int val, MathContext mc)
valueOfback to summary
public static Decimal valueOf(long val)

Hides java.math.BigDecimal.valueOf.

Doc from java.math.BigDecimal.valueOf.

Translates a long value into a BigDecimal with a scale of zero.

Parameters
val:long

value of the BigDecimal.

Returns:Decimal

a BigDecimal whose value is val.

valueOfback to summary
public static Decimal valueOf(long val, MathContext mc)
valueOfback to summary
public static Decimal valueOf(double val)

Hides java.math.BigDecimal.valueOf.

Doc from java.math.BigDecimal.valueOf.

Translates a double into a BigDecimal, using the double's canonical string representation provided by the Double#toString(double) method.

Parameters
val:double

double to convert to a BigDecimal.

Returns:Decimal

a BigDecimal whose value is equal to or approximately equal to the value of val.

valueOfback to summary
public static Decimal valueOf(double val, MathContext mc)
valueOfback to summary
public static Decimal valueOf(BigDecimal val)
valueOfback to summary
public static Decimal valueOf(BigDecimal val, MathContext mc)
valueOfback to summary
public static Decimal valueOf(String val)
valueOfback to summary
public static Decimal valueOf(String val, MathContext mc)
com.amazon.ion back to summary

private final Class Decimal.NegativeZero

extends Decimal
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final long
Inherited from com.amazon.ion.Decimal:
NEGATIVE_ZEROZERO

Constructor Summary

AccessConstructor and Description
private
NegativeZero(int scale)

private
NegativeZero(int scale, MathContext mc)

Method Summary

Modifier and TypeMethod and Description
public BigDecimal
abs()

Overrides java.math.BigDecimal.abs.

Returns a BigDecimal whose value is the absolute value of this BigDecimal, and whose scale is this.scale().

public BigDecimal
abs(MathContext
the context to use.
mc
)

Overrides java.math.BigDecimal.abs.

Returns a BigDecimal whose value is the absolute value of this BigDecimal, with rounding according to the context settings.

public double
doubleValue()

Overrides java.math.BigDecimal.doubleValue.

Converts this BigDecimal to a double.

public float
floatValue()

Overrides java.math.BigDecimal.floatValue.

Converts this BigDecimal to a float.

public String
toEngineeringString()

Overrides java.math.BigDecimal.toEngineeringString.

Returns a string representation of this BigDecimal, using engineering notation if an exponent is needed.

public String
toPlainString()

Overrides java.math.BigDecimal.toPlainString.

Returns a string representation of this BigDecimal without an exponent field.

public String
toString()

Overrides java.math.BigDecimal.toString.

Returns the string representation of this BigDecimal, using scientific notation if an exponent is needed.

Inherited from com.amazon.ion.Decimal:
bigDecimalValuebigDecimalValueequalsisNegativeZeroisNegativeZeronegativeZeronegativeZerovalueOfvalueOfvalueOfvalueOfvalueOfvalueOfvalueOfvalueOfvalueOfvalueOfvalueOfvalueOfvalueOfvalueOf

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides com.amazon.ion.Decimal.serialVersionUID.

Constructor Detail

NegativeZeroback to summary
private NegativeZero(int scale)
NegativeZeroback to summary
private NegativeZero(int scale, MathContext mc)

Method Detail

absback to summary
public BigDecimal abs()

Overrides java.math.BigDecimal.abs.

Doc from java.math.BigDecimal.abs.

Returns a BigDecimal whose value is the absolute value of this BigDecimal, and whose scale is this.scale().

Returns:BigDecimal

abs(this)

Annotations
@Override
absback to summary
public BigDecimal abs(MathContext mc)

Overrides java.math.BigDecimal.abs.

Doc from java.math.BigDecimal.abs.

Returns a BigDecimal whose value is the absolute value of this BigDecimal, with rounding according to the context settings.

Parameters
mc:MathContext

the context to use.

Returns:BigDecimal

abs(this), rounded as necessary.

Annotations
@Override
doubleValueback to summary
public double doubleValue()

Overrides java.math.BigDecimal.doubleValue.

Doc from java.math.BigDecimal.doubleValue.

Converts this BigDecimal to a double. This conversion is similar to the narrowing primitive conversion from double to float as defined in The Java Language Specification: if this BigDecimal has too great a magnitude represent as a double, it will be converted to Double#NEGATIVE_INFINITY or Double#POSITIVE_INFINITY as appropriate. Note that even when the return value is finite, this conversion can lose information about the precision of the BigDecimal value.

Returns:double

this BigDecimal converted to a double.

Annotations
@Override
floatValueback to summary
public float floatValue()

Overrides java.math.BigDecimal.floatValue.

Doc from java.math.BigDecimal.floatValue.

Converts this BigDecimal to a float. This conversion is similar to the narrowing primitive conversion from double to float as defined in The Java Language Specification: if this BigDecimal has too great a magnitude to represent as a float, it will be converted to Float#NEGATIVE_INFINITY or Float#POSITIVE_INFINITY as appropriate. Note that even when the return value is finite, this conversion can lose information about the precision of the BigDecimal value.

Returns:float

this BigDecimal converted to a float.

Annotations
@Override
toEngineeringStringback to summary
public String toEngineeringString()

Overrides java.math.BigDecimal.toEngineeringString.

Doc from java.math.BigDecimal.toEngineeringString.

Returns a string representation of this BigDecimal, using engineering notation if an exponent is needed.

Returns a string that represents the BigDecimal as described in the toString() method, except that if exponential notation is used, the power of ten is adjusted to be a multiple of three (engineering notation) such that the integer part of nonzero values will be in the range 1 through 999. If exponential notation is used for zero values, a decimal point and one or two fractional zero digits are used so that the scale of the zero value is preserved. Note that unlike the output of toString(), the output of this method is not guaranteed to recover the same [integer, scale] pair of this BigDecimal if the output string is converting back to a BigDecimal using the string constructor. The result of this method meets the weaker constraint of always producing a numerically equal result from applying the string constructor to the method's output.

Returns:String

string representation of this BigDecimal, using engineering notation if an exponent is needed.

Annotations
@Override
toPlainStringback to summary
public String toPlainString()

Overrides java.math.BigDecimal.toPlainString.

Doc from java.math.BigDecimal.toPlainString.

Returns a string representation of this BigDecimal without an exponent field. For values with a positive scale, the number of digits to the right of the decimal point is used to indicate scale. For values with a zero or negative scale, the resulting string is generated as if the value were converted to a numerically equal value with zero scale and as if all the trailing zeros of the zero scale value were present in the result. The entire string is prefixed by a minus sign character '-' ('\u002D') if the unscaled value is less than zero. No sign character is prefixed if the unscaled value is zero or positive. Note that if the result of this method is passed to the string constructor, only the numerical value of this BigDecimal will necessarily be recovered; the representation of the new BigDecimal may have a different scale. In particular, if this BigDecimal has a negative scale, the string resulting from this method will have a scale of zero when processed by the string constructor. (This method behaves analogously to the toString method in 1.4 and earlier releases.)

Returns:String

a string representation of this BigDecimal without an exponent field.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.math.BigDecimal.toString.

Doc from java.math.BigDecimal.toString.

Returns the string representation of this BigDecimal, using scientific notation if an exponent is needed.

A standard canonical string form of the BigDecimal is created as though by the following steps: first, the absolute value of the unscaled value of the BigDecimal is converted to a string in base ten using the characters '0' through '9' with no leading zeros (except if its value is zero, in which case a single '0' character is used).

Next, an adjusted exponent is calculated; this is the negated scale, plus the number of characters in the converted unscaled value, less one. That is, -scale+(ulength-1), where ulength is the length of the absolute value of the unscaled value in decimal digits (its precision).

If the scale is greater than or equal to zero and the adjusted exponent is greater than or equal to -6, the number will be converted to a character form without using exponential notation. In this case, if the scale is zero then no decimal point is added and if the scale is positive a decimal point will be inserted with the scale specifying the number of characters to the right of the decimal point. '0' characters are added to the left of the converted unscaled value as necessary. If no character precedes the decimal point after this insertion then a conventional '0' character is prefixed.

Otherwise (that is, if the scale is negative, or the adjusted exponent is less than -6), the number will be converted to a character form using exponential notation. In this case, if the converted BigInteger has more than one digit a decimal point is inserted after the first digit. An exponent in character form is then suffixed to the converted unscaled value (perhaps with inserted decimal point); this comprises the letter 'E' followed immediately by the adjusted exponent converted to a character form. The latter is in base ten, using the characters '0' through '9' with no leading zeros, and is always prefixed by a sign character '-' ('\u002D') if the adjusted exponent is negative, '+' ('\u002B') otherwise).

Finally, the entire string is prefixed by a minus sign character '-' ('\u002D') if the unscaled value is less than zero. No sign character is prefixed if the unscaled value is zero or positive.

Examples:

For each representation [unscaled value, scale] on the left, the resulting string is shown on the right.

[123,0]      "123"
[-123,0]     "-123"
[123,-1]     "1.23E+3"
[123,-3]     "1.23E+5"
[123,1]      "12.3"
[123,5]      "0.00123"
[123,10]     "1.23E-8"
[-123,12]    "-1.23E-10"
Notes:
  1. There is a one-to-one mapping between the distinguishable BigDecimal values and the result of this conversion. That is, every distinguishable BigDecimal value (unscaled value and scale) has a unique string representation as a result of using toString. If that string representation is converted back to a BigDecimal using the BigDecimal(String) constructor, then the original value will be recovered.
  2. The string produced for a given number is always the same; it is not affected by locale. This means that it can be used as a canonical string representation for exchanging decimal data, or as a key for a Hashtable, etc. Locale-sensitive number formatting and parsing is handled by the java.text.NumberFormat class and its subclasses.
  3. The toEngineeringString method may be used for presenting numbers with exponents in engineering notation, and the setScale method may be used for rounding a BigDecimal so it has a known number of digits after the decimal point.
  4. The digit-to-character mapping provided by Character.forDigit is used.
Returns:String

string representation of this BigDecimal.

Annotations
@Override