web接口中BigDecimal值比较不相等

BigDecimal oldPrice=new BigDecimal(50);
        BigDecimal newPrice=new BigDecimal(50.00);

        out.println("直接比较"+oldPrice.equals(newPrice));
        out.println("直接比较"+newPrice.equals(oldPrice));
        out.println("比较float值:"+(oldPrice.floatValue()==newPrice.floatValue()));
        out.println("比较double值:"+(oldPrice.doubleValue()==newPrice.doubleValue()));

        BigDecimal oldPrice1=new BigDecimal("50.1");
        BigDecimal newPrice1=new BigDecimal("50.10");

        out.println("直接比较"+oldPrice1.equals(newPrice1));
        out.println("直接比较"+newPrice1.equals(oldPrice1));
        out.println("比较float值:"+(oldPrice1.floatValue()==newPrice1.floatValue()));
        out.println("比较double值:"+(oldPrice1.doubleValue()==newPrice1.doubleValue()));

输出结果code

直接比较true
直接比较true
比较float值:true
比较double值:true
直接比较false
直接比较false
比较float值:true
比较double值:true

当从后台接受BigDecimal是从string字符串转成的,因此会保留小数点.若是原价是3.90,新价是3.9,则认为它两不相等.ci

相关文章
相关标签/搜索