Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Compare Two Numbers in Java?
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/2683202/comparing-the-values-of-two-generic-numbers">Comparing the values of two generic Numbers</a> </p> </blockquote> <p>I would like to compare two arbitrary instances of <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Number.html" rel="nofollow noreferrer">Number</a> to find out which is greater. As you know, those may be Integers, Longs or Doubles but also BigIntegers or BigDecimals. So what to do?</p> <ol> <li>Number does not implement <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html" rel="nofollow noreferrer">Comparable</a>.</li> <li>Can't use <code>doubleValue()</code> as there might be a loss of precision from BigDecimals or BigIntegers.</li> </ol> <p>Do I really have to go into instance-testing and casting? There <em>must</em> be some library that does this. But I've already checked Apache Commons and Guava with no luck.</p> <p><strong>Edit 1:</strong> Comments pointed this out: <a href="https://stackoverflow.com/q/2683202/1374678">Comparing the values of two generic Numbers</a>. It has a solution that converts everything to a BigDecimal using <code>toString()</code>. Is there really no better way to do this?</p> <p><strong>Edit 2:</strong> I just posted <a href="https://stackoverflow.com/a/12884075/1374678">my own version as an answer to the other question</a>. It should be more efficient and robust than the accepted answer.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload