【effective java】48.如果需要精确的答案,请避免使用float和double

float和double主要为了科学计算和工程计算而设计,执行二进制浮点运算,这是为了在广泛的数值范围上提供较为精确的快速近似计算而精心设计的。然而,它们没有提供完全精确的结果,所以不适合用于需要精确结果的场合,尤其是货币计算。

public static void main(String[] args) {
    //假设有1.03元,花掉0.42元后
    System.out.println(1.03 - .42);//0.6100000000000001
    System.out.println(1.00 - 9 * .10);//0.09999999999999998
    System.out.println("=========");
    test1();
    System.out.println("=========");
    test2();
    System.out.println("=========");
    /**
     * 使用BigDecimal的缺点是:1.与基本类型相比,不方便(需要创建BigDecimal对象);2.速度慢
        使用int或者long,取决于涉及的数值大小,同时要自己处理十进制小数,以分为单位,而不是以元为单位计算,就可以使用int来处理:
     */
    test3();
}
private static void test1() {
    // TODO Auto-generated method stub
    double funds = 1.00;
    int itemsBought = 0;
    for(double price = .10; funds >= price; price += .10) {
        funds -= price;
        itemsBought++;
        System.out.println("price:"+price+"funds:"+funds);
    }
    System.out.println(itemsBought + " items bought.");
    System.out.println("Money left over: $" + funds);
}

private static void test2() {
    final BigDecimal TEN_CENTS = new BigDecimal(".10");
    int itemsBought = 0;
    BigDecimal funds = new BigDecimal("1.00");
    for(BigDecimal price = TEN_CENTS; funds.compareTo(price) >= 0; price = price.add(TEN_CENTS)) {
        itemsBought++;
        funds = funds.subtract(price);
        System.out.println("price:"+price+"funds:"+funds);
    }
    System.out.println(itemsBought + " items bought.");
    System.out.println("Money left over: $" + funds);

}


private static void test3() {
    // TODO Auto-generated method stub
    int itemsBought = 0;
    int funds = 100;
    for(int price = 10; funds >= price; price += 10) {
        itemsBought++;
        funds -= price;
        System.out.println("price:"+price+"funds:"+funds);
    }
    System.out.println(itemsBought + " items bought.");
    System.out.println("Money left over: $" + funds);
}

总结:对于需要精确答案的计算,不能使用float或者double,BigDecimal允许完全控制舍入,如果业务要求涉及多种舍入方式,使用BigDecimal很方便,如果性能很关键,涉及的数值不大,就可以使用int或者float,如果数值范围没有超过9位十进制数字,可以使用int,如果不超过18位数字,使用long,如果数值可能超过18位,就必须用BigDecimal。

猜你喜欢

转载自blog.csdn.net/CharJay_Lin/article/details/81428864
今日推荐