JAVA-比较浮点型数据

Float

public static void main(String[] args) {
    Float x = 12.4F;
    Float y = 12.4F;

    // 比较对象地址
    System.out.println(x == y);

    // 比较值,不准确,会丢失精度
    System.out.println(x.equals(y));

    // x 与 y 的绝对值
    System.out.println(Math.abs(x - y));

    // 比较精度比值的精度多一位即可
    if (Math.abs(x - y) > .01) {
        System.out.println("不相等");
    } else {
        System.out.println("相等");
    }
}


https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.4

猜你喜欢

转载自www.cnblogs.com/jhxxb/p/10530457.html
今日推荐