Class Numbers

java.lang.Object
com.globalmentor.java.Numbers

public class Numbers extends Object
Utilities for manipulating numbers.
Author:
Garret Wilson
  • Field Details

    • ONE_THIRD_DOUBLE

      public static final double ONE_THIRD_DOUBLE
      The double value representing 1/3.
      See Also:
    • TWO_THIRDS_DOUBLE

      public static final double TWO_THIRDS_DOUBLE
      The double value representing 2/3.
      See Also:
  • Constructor Details

    • Numbers

      public Numbers()
  • Method Details

    • asIntegralValue

      public static long asIntegralValue(Number number)
      Returns the value of the given number if the number is integral.
      Parameters:
      number - The number to examine.
      Returns:
      The value of the given number if the number is integral, or -1 if the given number is not an integral.
      See Also:
    • asDecimalValue

      public static double asDecimalValue(Number number)
      Returns the value of the given number if the number is decimal.
      Parameters:
      number - The number to examine.
      Returns:
      The value of the given number if the number is decimal, or -1 if the given number is not decimal.
      See Also:
    • isIntegral

      public static boolean isIntegral(Number number)
      Determines if the given number is an integral type: BigInteger, Byte, Integer, Long, or Short.
      Parameters:
      number - The number to examine.
      Returns:
      true if the given number is an integral type.
    • isDecimal

      public static boolean isDecimal(Number number)
      Determines if the given number is a non-integer decimal type: Float, Double, or BigDecimal.
      Parameters:
      number - The number to examine.
      Returns:
      true if the given number is a non-integer decimal type.
    • toBigDecimal

      public static BigDecimal toBigDecimal(Number number)
      Returns a big decimal representation of the given number. If the number is already a BigDecimal, the same number object is returned.
      Parameters:
      number - The number to return as a big decimal.
      Returns:
      A big decimal representing the number.
    • compare

      public static int compare(Number number1, Number number2)
      Compares the two specified numbers.
      Parameters:
      number1 - The first number to compare.
      number2 - The second number to compare.
      Returns:
      The value 0 if the first number is numerically equal to the second number; a value less than 0 if the first number is numerically less than the second number; or a value greater than 0 if the first number is numerically greater than the second number.
      Throws:
      NullPointerException - if either of the given numbers is null.
    • sort

      public static int sort(Object object1, Object object2)
      Compares the two specified objects, one of which must be an instance of Number, and either of which may be null. Numbers are sorted before non-numbers, and two numbers are compared.
      Parameters:
      object1 - The first object to compare.
      object2 - The second object to compare.
      Returns:
      The value 0 if both are numbers and the first number is numerically equal to the second number; a value less than 0 if only the first is a number, or both are numbers and the first number is numerically less than the second number; or a value greater than 0 if only the second is a number, or both are numbers and the first number is numerically greater than the second number.
      Throws:
      IllegalArgumentException - if neither of the given objects is an instance of Number.
    • sort

      public static int sort(Object object1, Object object2, boolean ambiguous)
      Compares the two specified objects, one of which must be an instance of Number, and either of which may be null. Numbers are sorted before non-numbers, and two numbers are compared. If ambiguous sorting is requested, if neither object is a number the objects will be considered equal; otherwise, if neither object is a number an exception will be thrown.
      Parameters:
      object1 - The first object to compare.
      object2 - The second object to compare.
      ambiguous - Whether ambiguous sorting is allowed.
      Returns:
      The value 0 if both are numbers and the first number is numerically equal to the second number; a value less than 0 if only the first is a number, or both are numbers and the first number is numerically less than the second number; or a value greater than 0 if only the second is a number, or both are numbers and the first number is numerically greater than the second number.
      Throws:
      IllegalArgumentException - if neither of the given objects is an instance of Number and non-ambiguous sorting is requested.