Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Number for zero java

I can't seem to figure out how to test a number to see if it's zero. Couldn't someone please tell me what I'm missing.

getMin = Number.class
getMax = Number.class

Test

Number value = mvFacet.getMin() > 0 ? mvFacet.getMin() : mvFacet.getMax();
System.out.println(value.toString());
like image 494
Code Junkie Avatar asked Feb 12 '26 19:02

Code Junkie


2 Answers

To add to Jason's answer, if you're going to use num.doubleValue() then don't use == since it doesn't handle edge cases like when the double value is NaN or Infinity. Please use Double.compare(num.doubleValue(), 0.0);.

like image 92
pretzelhammer Avatar answered Feb 15 '26 08:02

pretzelhammer


Number num;
...
if(num.intValue() == 0) {
    // num is 0 so do something here
}

Other methods available on Number are:

byteValue()
doubleValue()
floatValue()
longValue()
shortValue()

Select whichever method makes the most sense for the values you are using.

like image 23
Jason Avatar answered Feb 15 '26 09:02

Jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!