As title suggests, how do i check if particular Double
is negative or not. Here is how am getting Double
instance
(Double.parseDouble(data[2])
Thoughts, suggestions?
As title suggests, how do i check if particular Double
is negative or not. Here is how am getting Double
instance
(Double.parseDouble(data[2])
Thoughts, suggestions?
You could test if it is
< 0
:Doesn't give you a
Double
, it returns adouble
. If you are assigning it to aDouble
, it gets there via autoboxing. Anyways to see if it is negative compare it to 0? As in:Being pedantic,
< 0
won't give you all negative numbers.prints
-0.0
is negative but not less than0.0
You can use
A more efficient, if more obtuse, version is to check the signed bit.
Note: Under IEEE-754 a NaN can have the same signed bit as a negative number.
Another possible solution that I quite like is
Double.parseDouble
returns adouble
(primitive) not aDouble
. In this case it doesn't really matter, but it's worth being aware of.You can use:
to check whether a value is negative - but be aware that this isn't the opposite of
due to "not a number" values. It does work with infinite values though.