public interface JsonNumber extends JsonValue
Implementations may use a BigDecimal object to store the numeric
value internally.
The BigDecimal object can be constructed from the following types:
int BigDecimal(int),
long BigDecimal(long),
BigInteger BigDecimal(BigInteger),
double BigDecimal.valueOf(double), and
String BigDecimal(String).
Some of the method semantics in this class are defined using the
BigDecimal semantics.
JsonValue.ValueTypeEMPTY_JSON_ARRAY, EMPTY_JSON_OBJECT, FALSE, NULL, TRUE| Modifier and Type | Method and Description |
|---|---|
java.math.BigDecimal |
bigDecimalValue()
Returns this JSON number as a
BigDecimal object. |
java.math.BigInteger |
bigIntegerValue()
Returns this JSON number as a
BigInteger object. |
java.math.BigInteger |
bigIntegerValueExact()
Returns this JSON number as a
BigInteger object. |
double |
doubleValue()
Returns this JSON number as a
double. |
boolean |
equals(java.lang.Object obj)
Compares the specified object with this
JsonNumber object for
equality. |
int |
hashCode()
Returns the hash code value for this
JsonNumber object. |
int |
intValue()
Returns this JSON number as an
int. |
int |
intValueExact()
Returns this JSON number as an
int. |
boolean |
isIntegral()
Returns true if this JSON number is a integral number.
|
long |
longValue()
Returns this JSON number as a
long. |
long |
longValueExact()
Returns this JSON number as a
long. |
default java.lang.Number |
numberValue()
Returns this JSON number as a
Number object. |
java.lang.String |
toString()
Returns a JSON text representation of the JSON number.
|
asJsonArray, asJsonObject, getValueTypeboolean isIntegral()
bigDecimalValue().scale(). If the
scale is zero, then it is considered integral type. This integral type
information can be used to invoke an appropriate accessor method to
obtain a numeric value as in the following example:
JsonNumber num = ...
if (num.isIntegral()) {
num.longValue(); // or other methods to get integral value
} else {
num.doubleValue(); // or other methods to get decimal number value
}
int intValue()
int. Note that this conversion
can lose information about the overall magnitude and precision of the
number value as well as return a result with the opposite sign.int representation of the JSON numberBigDecimal.intValue()int intValueExact()
int.int representation of the JSON numberjava.lang.ArithmeticException - if the number has a nonzero fractional
part or if it does not fit in an intBigDecimal.intValueExact()long longValue()
long. Note that this conversion
can lose information about the overall magnitude and precision of the
number value as well as return a result with the opposite sign.long representation of the JSON number.BigDecimal.longValue()long longValueExact()
long.long representation of the JSON numberjava.lang.ArithmeticException - if the number has a non-zero fractional
part or if it does not fit in a longBigDecimal.longValueExact()java.math.BigInteger bigIntegerValue()
BigInteger object. This is a
a convenience method for bigDecimalValue().toBigInteger().
Note that this conversion can lose information about the overall
magnitude and precision of the number value as well as return a result
with the opposite sign.BigInteger representation of the JSON number.BigDecimal.toBigInteger()java.math.BigInteger bigIntegerValueExact()
BigInteger object. This is a
convenience method for bigDecimalValue().toBigIntegerExact().BigInteger representation of the JSON numberjava.lang.ArithmeticException - if the number has a nonzero fractional partBigDecimal.toBigIntegerExact()double doubleValue()
double. This is a
a convenience method for bigDecimalValue().doubleValue().
Note that this conversion can lose information about the overall
magnitude and precision of the number value as well as return a result
with the opposite sign.double representation of the JSON numberBigDecimal.doubleValue()java.math.BigDecimal bigDecimalValue()
BigDecimal object.BigDecimal representation of the JSON numberdefault java.lang.Number numberValue()
Number object.Number representation of the JSON numberjava.lang.String toString()
BigDecimal.toString().boolean equals(java.lang.Object obj)
JsonNumber object for
equality. Returns true if and only if the type of the specified
object is also JsonNumber and their bigDecimalValue()
objects are equalequals in class java.lang.Objectobj - the object to be compared for equality with
this JsonNumbertrue if the specified object is equal to this
JsonNumberint hashCode()
JsonNumber object. The
hash code of a JsonNumber object is defined as the hash code of
its bigDecimalValue() object.hashCode in class java.lang.ObjectJsonNumber object