I know there're already a lot of questions like this. But what if I need to compare 2 Numbers and I don't know what types they are. Which means one of them could be Float and another one be Long. Are there some libs in JavaCore or Apache that can solve this issue (unfortunalety NumberUtils lacks this method)? I've been looking for a few hours but haven't succeeded. Or I should reinvent the wheel with something like this. I just don't believe that such high-level language like Java doesn't have tools to compare 2 numbers.
-
Mind sharing the code? – Afzaal Ahmad Zeeshan Oct 09 '14 at 16:17
-
both Object and Number don't have comparable, this operator is overloaded only for comparable objects. Or for for primitive types. – deathangel908 Oct 09 '14 at 16:19
-
You could try get whatever value (int,float,double) and cast them to whatever data type e.g Float and perform your comparison with a data type you know – James Wahome Oct 09 '14 at 16:19
-
1<, >, <=, >=, ==, !=. – MarGar Oct 09 '14 at 16:19
-
for comparing numbers you have `<`,`>` and `!` use it. – Rustam Oct 09 '14 at 16:20
-
http://stackoverflow.com/questions/3214448/comparing-numbers-in-java – nem035 Oct 09 '14 at 16:20
-
Which means I should reinvent the wheel. What if max range of one of them is greater than other? What if I cast float to long and distort the result. Realization of this method should calculate all of those – deathangel908 Oct 09 '14 at 16:21
-
"Java doesn't have tools to compare 2 numbers." wtf? – Madhawa Priyashantha Oct 09 '14 at 16:22
-
You said you know there are already a lot of questions like this. So how is your question different from any of the others, such as http://stackoverflow.com/q/2683202, which seems identical to me? – Dawood ibn Kareem Oct 09 '14 at 16:30
-
@Eran have you tried it either? (when x is Long and y is Float as the post mentions..) – Serhiy Oct 09 '14 at 16:42
-
@David Wallace I saw it. This compares both of type T extends Number. So if the 1st one is Long the second should be only long which is no problem. – deathangel908 Oct 09 '14 at 20:01
2 Answers
You can use the doubleValue() method on both numbers and then compare the double values.
If you need a higher precision for integers, you can implement this as sepcial case.
If you have objects that are not an instance of Number (e.g. Object or String containing a "number"), then you should throw all your code away and start from scratch, honestly.

- 2,419
- 17
- 24
The int will be converted to a long before the compare is made. In principle a cast will take place, like
if (a == (long)b)
Java will cast primative types for you, provided that it does not involve a loss of precision. Since a long can hold all the values an int can, the cast will be performed automatically, but you would have to explicitly cast (for example) a long if you wanted to use it where an int should be.
edit: to cast a long "x" to an int, use: x = (int)x;

- 909
- 7
- 11
-
What if I have Long 3l and Float 3.3f ? if I cast Fload to Long it Will evaluate that 3L=3.3f – deathangel908 Oct 09 '14 at 19:57