Class JsonDecimal

java.lang.Object
ca.marcusdunn.jsonlens.model.JsonDecimal
All Implemented Interfaces:
Comparable<JsonDecimal>

public final class JsonDecimal extends Object implements 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 Details

    • ZERO

      public static final JsonDecimal ZERO
      The value zero.
  • Method Details

    • of

      public static JsonDecimal of(long value)
      Returns the decimal of a long value.
      Parameters:
      value - the value
      Returns:
      the decimal
    • of

      public static JsonDecimal of(BigInteger value)
      Returns the decimal of a BigInteger value.
      Parameters:
      value - the value
      Returns:
      the decimal
    • of

      public static JsonDecimal of(BigDecimal value)
      Returns the decimal of a BigDecimal value. The scale of the value has no effect, so 1.50 and 1.5 give equal decimals.
      Parameters:
      value - the value
      Returns:
      the decimal
    • parse

      public static Maybe<JsonDecimal> parse(CharSequence text)

      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.None if 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

      public String 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

      public BigInteger exponent()
      Returns the exponent of ten.
      Returns:
      the exponent. For zero, the exponent is zero.
    • toBigDecimal

      public Maybe<BigDecimal> toBigDecimal()
      Returns the value as a BigDecimal, if a BigDecimal can hold it.
      Returns:
      the value, or Maybe.None if the exponent is outside the range of the scale of a BigDecimal (an int)
    • compareTo

      public int compareTo(JsonDecimal other)
      Compares two decimals by their mathematical values.
      Specified by:
      compareTo in interface Comparable<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

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Returns the value as the text of a JSON number, for example -125E-1.
      Overrides:
      toString in class Object
      Returns:
      the text. parse(CharSequence) of the text gives an equal decimal.