Class JsonDecimal
java.lang.Object
ca.marcusdunn.jsonlens.model.JsonDecimal
- All Implemented Interfaces:
Comparable<JsonDecimal>
An exact decimal number with no limit on its size or on its exponent.
Each JSON number has exactly one JsonDecimal. JsonNumber.exactValue() gives it, so that
two numbers in different representations can be compared exactly.
The value is signum × significand × 10^exponent:
| Number | signum() |
significand() |
exponent() |
|---|---|---|---|
0 |
0 | "0" |
0 |
-12.50 |
-1 | "125" |
-1 |
1e400 |
1 | "1" |
400 |
1e99999999999 |
1 | "1" |
99999999999 |
The form is canonical: the significand has no leading zeros and no trailing zeros. Thus two
decimals are equal if and only if compareTo(JsonDecimal) gives zero.
JsonDecimal a = JsonDecimal.of(new BigDecimal("1.50"));
JsonDecimal b = JsonDecimal.parse("15e-1").orElse(JsonDecimal.ZERO);
assert a.equals(b);
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionintcompareTo(JsonDecimal other) Compares two decimals by their mathematical values.booleanexponent()Returns the exponent of ten.inthashCode()static JsonDecimalof(long value) Returns the decimal of alongvalue.static JsonDecimalof(BigDecimal value) Returns the decimal of aBigDecimalvalue.static JsonDecimalof(BigInteger value) Returns the decimal of aBigIntegervalue.static Maybe<JsonDecimal> parse(CharSequence text) Reads the text of a JSON number (RFC 8259, Section 6).Returns the digits of the significand.intsignum()Returns the sign.Returns the value as aBigDecimal, if aBigDecimalcan hold it.toString()Returns the value as the text of a JSON number, for example-125E-1.
-
Field Details
-
ZERO
The value zero.
-
-
Method Details
-
of
Returns the decimal of alongvalue.- Parameters:
value- the value- Returns:
- the decimal
-
of
Returns the decimal of aBigIntegervalue.- Parameters:
value- the value- Returns:
- the decimal
-
of
Returns the decimal of aBigDecimalvalue. The scale of the value has no effect, so1.50and1.5give equal decimals.- Parameters:
value- the value- Returns:
- the decimal
-
parse
Reads the text of a JSON number (RFC 8259, Section 6).
The exponent can have any number of digits. The text must contain only the number: no spaces, no leading
+, and no leading zeros.- Parameters:
text- the text of a JSON number, for example-12.5e-3- Returns:
- the decimal, or
Maybe.Noneif the text is not a JSON number
-
signum
public int signum()Returns the sign.- Returns:
- -1, 0, or 1 if the value is negative, zero, or positive
-
significand
Returns the digits of the significand.- Returns:
- the decimal digits, without leading zeros and without trailing zeros. For zero,
the text is
"0".
-
exponent
Returns the exponent of ten.- Returns:
- the exponent. For zero, the exponent is zero.
-
toBigDecimal
Returns the value as aBigDecimal, if aBigDecimalcan hold it.- Returns:
- the value, or
Maybe.Noneif the exponent is outside the range of the scale of aBigDecimal(anint)
-
compareTo
Compares two decimals by their mathematical values.- Specified by:
compareToin interfaceComparable<JsonDecimal>- Parameters:
other- another decimal- Returns:
- -1, 0, or 1 if this value is less than, equal to, or greater than the other value
-
equals
-
hashCode
-
toString
Returns the value as the text of a JSON number, for example-125E-1.- Overrides:
toStringin classObject- Returns:
- the text.
parse(CharSequence)of the text gives an equal decimal.
-