I have a method with a Number parameter and have to determine if it is smaller than an int. What I came up with is this :
Integer.valueOf(myInt) > (Integer) myNumber
Which looks rather clumsy for such a simple task. Moreover, I am not sure if it would play nicely with BigDecimal et.al., and what cases would I have to test?
How could it be improved?
Thank you
Your code will result in a
ClassCastExceptionif themyNumberis anything but anInteger.I’d say this has the best chance of dealing correctly with all
Numbertypes:because
doublehas the widest range of all the types you can convert aNumberto, and it will not truncate fractions.