public class FloatTest {

    public static void main(String[] args) {

        float a = 1.0f - 0.9f;
        float b = 0.9f - 0.8f;

        System.out.println("a" + a);
        System.out.println("b" + b);

        Float x = Float.valueOf(a);
        Float y = Float.valueOf(b);

        System.out.println("x" + x);
        System.out.println("y" + y);

        System.out.println(" -----------上方为错误示范---------- ");
        System.out.println(" ---------- 正确使用.float ----------- ");
        float c = 1.0f - 0.9f;
        float d = 0.9f - 0.8f;

        // 1e-6f 不能有空格,不然报错
        // 指定一个误差范围,两个浮点数的差值在此范围之内,则认为是相等的。
        float diff = 1e-6f;
        if (Math.abs(c-d)<diff){
            System.out.println("true");
        }

        System.out.println(" ---------- 正确使用.BigDecimal ----------- ");
        BigDecimal e = new BigDecimal("1.0");
        BigDecimal f = new BigDecimal("0.9");
        BigDecimal g = new BigDecimal("0.8");

        BigDecimal o = e.subtract(f);
        BigDecimal p = f.subtract(g);
        System.out.println("BigDecimal o " + o);
        System.out.println("BigDecimal p " + p);
        if (o.equals(p)){
            System.out.println("true");
        }
    }

}