Java Utililty Methods Number Compare

List of utility methods to do Number Compare

Description

The list of methods to do Number Compare are organized into topic(s).

Method

intcompare(final Number x, final Number y)
Compare two numbers of arbitrary data types
if (isSpecial(x) || isSpecial(y)) {
    return Double.compare(x.doubleValue(), y.doubleValue());
} else {
    return toBigDecimal(x).compareTo(toBigDecimal(y));
intcompare(Object o1, Object o2)
compare
int result = 0;
if (o1 != null) {
    result = 1;
    if (o2 != null) {
        if (o1 instanceof String) {
            result = ((String) o1).compareTo((String) o2);
        } else if (o1 instanceof Integer) {
            result = ((Integer) o1).compareTo((Integer) o2);
...
intcompareDoubleAgainstLong(double lhs, long rhs)
compare Double Against Long
if ((NON_DOUBLE_LONG & rhs) != NON_DOUBLE_LONG) {
    if (Double.isNaN(lhs)) {
        return +1;
    if (Double.isInfinite(lhs)) {
        return lhs < 0 ? -1 : +1;
    return BigDecimal.valueOf(lhs).compareTo(BigDecimal.valueOf(rhs));
...
booleancompareDoublesWithTolerance(double aa, double bb, double tolerance)
compare Doubles With Tolerance
if (Double.compare(aa, bb) == 0)
    return true;
return Math.abs(aa - bb) < tolerance;
booleancompareStringsAsNumbers(String s1, String s2, String op)
compare Strings As Numbers
int result = new BigDecimal(s1).compareTo(new BigDecimal(s2));
switch (op) {
case "<":
    return result < 0;
case "<=":
    return result <= 0;
case ">":
    return result > 0;
...
intcompareTo(double v1, double v2)
compare To
BigDecimal b1 = new BigDecimal(v1);
BigDecimal b2 = new BigDecimal(v2);
return b1.compareTo(b2);
intcompareTo(Object val1, Object val2)
compare To
return new BigDecimal(val1.toString()).setScale(COMPARE_SCALE, ROUNDING_MODE)
        .compareTo(new BigDecimal(val2.toString()).setScale(COMPARE_SCALE, ROUNDING_MODE));
intcompareTo(Object value1, Object value2)
compare To
return new BigDecimal(String.valueOf(value1)).compareTo(new BigDecimal(String.valueOf(value2)));
intcompareTwoObject(Object o1, Object o2)
Compare two object
Class objClass;
if (o1 == null && o2 == null) {
    return 0;
} else if (o1 == null) {
    return -1;
} else if (o2 == null) {
    return 1;
objClass = o1.getClass();
if (o1 instanceof Comparable && o2 instanceof Comparable) {
    int result = ((Comparable) o1).compareTo(o2);
    return normalizeResult(result);
} else if (objClass.getSuperclass() == Number.class) {
    Number n1 = (Number) o1;
    double d1 = n1.doubleValue();
    Number n2 = (Number) o2;
    double d2 = n2.doubleValue();
    if (d1 < d2) {
        return -1;
    } else if (d1 > d2) {
        return 1;
    } else {
        return 0;
} else if (objClass == Boolean.class) {
    Boolean bool1 = (Boolean) o1;
    boolean b1 = bool1.booleanValue();
    Boolean bool2 = (Boolean) o2;
    boolean b2 = bool2.booleanValue();
    if (b1 == b2) {
        return 0;
    } else if (b1) {
        return 1;
    } else {
        return -1;
} else if (objClass == BigDecimal.class) {
    BigDecimal n1 = (BigDecimal) o1;
    double d1 = n1.doubleValue();
    BigDecimal n2 = (BigDecimal) o2;
    double d2 = n2.doubleValue();
    if (d1 < d2) {
        return -1;
    } else if (d1 > d2) {
        return 1;
    } else {
        return 0;
} else {
    String s1 = o1.toString();
    String s2 = o2.toString();
    int result = s1.compareTo(s2);
    return normalizeResult(result);
intcompareValue(Object left, Object right)
compare Value
return compareValue(left, right, true);