The comparison gets a little tricky because BigDecimal and BigInteger also extend Number. These classes can hold integer values of unlimited size (well, limited by the memory on your computer).
Thus, if you ask for for the double value or long value of these you may risk erroneous comparisons since BigDecimal or BigInteger will be forced to truncate their value.
The safest thing to do would be to convert the Number to a String and then give this String to the BigDecimal class to parse.
eg.
Number n = ...;
int i = ...;
BigDecimal m = new BigDecimal(n.toString());
BigDecimal j = new BigDecimal(i);
boolean result = j.compareTo(m) < 0;
// equivalent to i < n
If you're sure you will never get an instance of BigInteger or BigDecimal whose value exceeds the maximum positive or maximum negative value of a double then it should be safe to use Number.doubleValue to get a number to compare to.
There are additional problems that you may face with the possibility of having to compare BigDecimals. This is because BigDecimals represent their values in base 10, whereas other Number subclasses use base 2.
As such new BigDecimal("0.1")
is not equal to 0.1d
or 0.1f
. This is because floats and doubles cannot represent many base 10 fractions accurately (whereas BigDecimal can). So getting a double value from a BigDecimal may end up giving you erroneous comparisons. But since you are comparing to ints this in a problem that you do not need to face.