How do I check if a zero is positive or negative?

前端 未结 7 1320
孤街浪徒
孤街浪徒 2020-12-01 06:50

Is it possible to check if a float is a positive zero (0.0) or a negative zero (-0.0)?

I\'ve converted the float to a String a

7条回答
  •  独厮守ぢ
    2020-12-01 07:39

    The approach used by Math.min is similar to what Jesper proposes but a little clearer:

    private static int negativeZeroFloatBits = Float.floatToRawIntBits(-0.0f);
    
    float f = -0.0f;
    boolean isNegativeZero = (Float.floatToRawIntBits(f) == negativeZeroFloatBits);
    

提交回复
热议问题