Float greater than or less than zero

I have the following code that sometimes returns true, and sometimes not.

Any idea what might cause a variable result?

0.00 line comes from JSON object.

(simplified code)

if(new Float("0.00")>0){
    // do something
}

EDIT:

I have some float that I want to determine if its zero, less than zero or greater than zero. Values ​​may be 0.00, 0.001, or -0.001. How to determine if they are positive, negative or zero?

EDIT:

Perhaps I should clarify how I get the values. I could be completely different, which causes the problem, because I read about BigDecimal and tried to use it to no avail.

The values ​​are retrieved from the JSON channel (which has this format LT: 0,000) using this code:

price = new BigDecimal(stocksJSONArray.getJSONObject(i).getString("LT"));

, , , :

if(price.compareTo(BigDecimal.ZERO)==1){
    // greater than zero
}
else if(price.compareTo(BigDecimal.ZERO)==-1){
    // less than zero
}

, JSON. , price, , , . , - ?

, , . :

DecimalFormat frmt = new DecimalFormat("0.000000000000000000000000");
String formatted = frmt.format(stock.change);

, , 0.000000000000000000000000, 0.000000000000000000000001 - .

+3
4

false Java.

, - , , -1, . , , -0,00. ( ), 0,00.

, - , . .

.

. , . , .

. , - .

32 , 24 - 8 . , 0,001 0,00100001, 0,001000001, :

 System.out.println((float)0.001 < (float)0.001000001);

( , , . , - .)

, . . , , , , , .

, , , , .

  • . , 0 0.25. , .
  • , . , 1/3 0,1 0,001. , , , .
  • . denormal () . , , , , , Java , , , , .
  • , .

, : 0 (), 0.001 () -0.001 (), . 0- (, ) ( ).

, . 0,001, -0,001 (float)1000 * (float)0.001 - 1, , , :

if (-0.00001 < x && x < 0.00001) // then x is zero 

- , , , . 0.000000000... Java , Java- 0.00, , , . , , - , 3 .

+3

. , :

float result = new Float("0.00");
float expected = 0;
if (Math.abs(result - expected) < 0.00001)
...
+1

Math.signum(), , , .

0

I have changed a bit. I expected the sensor to read 1. This is what I came up with.

double a = y1;
                if((a-0)<1){
                    a=(1-a)+a;
                }
0
source

All Articles